A
A
Andrew2022-04-20 12:48:59
Vue.js
Andrew, 2022-04-20 12:48:59

How to set a default value for components?

There are two components Tabs and Tab , Tabs acts as a control for a group of several Tabs, it looks like this .
Is it possible to make it so that when the v-model is empty, the first Tab is set by default? I tried to implement this in the setup of the Tab component:

...
  setup(props) {
    const tabsGroupValue = inject("tabsGroupValue");

    const isSelected = computed(() => props.value === tabsGroupValue.value);
    
    if (!tabsGroupValue.value)
        tabsGroupValue.value = props.value;

    return {
      tabsGroupValue,
      isSelected,
    };
  },
...

This works, but for some reason the last Tab is set by default, and not the first. Is there any way to fix this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question