multiselect icon indicating copy to clipboard operation
multiselect copied to clipboard

How to use ref for multiselect with types and composition-api in Vue 3?

Open rezuankassim opened this issue 3 years ago • 1 comments

I currently have these codes

import Multiselect from '@vueform/multiselect'
...
setup() {
    const multiselect = ref<Multiselect | null>(null)
                            ^^^^^^^^^^^
// Error: Cannot use namespace 'Multiselect' as type
}

How do go around and do this? Sorry this is my first time using typescript. Thanks in advance.

rezuankassim avatar May 25 '22 08:05 rezuankassim

Hi, you can do like this const multiselect = ref<InstanceType<typeof MultiSelect>>(null);

EShebunin avatar Sep 02 '22 15:09 EShebunin

@EShebunin When I access clear() or other methods it not works

Property 'clear' does not exist on type 'Multiselect'

cnakh avatar Nov 23 '22 09:11 cnakh

I have same issue please help with this

rayanazzam1991 avatar Feb 19 '23 20:02 rayanazzam1991

@rayanazzam1991 simple workaround:

interface CustomMultiselect extends Multiselect{
    close: () => void;
    clear: () => void;
  }

xibman avatar Feb 20 '23 13:02 xibman

Could anyone reliably help me with TS issues to put everything in place once and for all in both Vue 2 & 3? If so, please let me know!

adamberecz avatar Mar 09 '23 12:03 adamberecz

I have same issue. I think it not support defineExpose ref for composition-api in Vue 3

tienanhbui avatar Mar 26 '23 05:03 tienanhbui

Same issue, and i think @tienanhbui is right. Is there any plan on adding this? Or is there any workaround to access the api methods?

n14s avatar Jun 14 '23 10:06 n14s

I have found a hacky workaround that works specifically for my use case, but might be adjusted to fit other useCases as well.

I need to access the API method refreshOptions.

As I can't access the method, i replicated the behaviour by rerendering the component.

    <div v-if="isRendered">
      <Multiselect
        v-model="value"
        :placeholder="placeholder"
        :resolve-on-load="true"
        :searchable="true"
        :options="getData"
        class="rs-form rs-multiselect"
        :mode="mode"
        :no-options-text="noOptionsText"
      />
    </div>
const isRendered = ref(true);
const refreshOptions = async () => {
  isRendered.value = false;
  await nextTick();
  isRendered.value = true;
};

Now, whenever I want to refresh the options, I call my custom refreshOptions function. Note, this only works, because I use resolve-on-load="true"

Hope this helps some people. I also hope a proper way for using API methods with vue 3 is added soon.

n14s avatar Jun 14 '23 13:06 n14s

This should be fixed with the upcoming release. If not, feel free to reopen.

adamberecz avatar Oct 06 '23 14:10 adamberecz