#defineModel modifier doesnt return "modified" value Vue

18 messages · Page 1 of 1 (latest)

mystic helm
#

Hello, i'm using the new defineModel in the latest version of vue using Nuxt, in my custom component i add the model as "v-model:item.split" however the modifier doesnt seem to apply the effect. This is an example which i'm trying to work with. Am i doing something wrong here?

The setter function doesnt seem to work at all , i tried using some random return value on the outside scoped return and it still doesnt work in my template

const [item, modifier] = defineModel<ItemAnnotation>("item", {
  set(value) {
    if (modifier.split) {
      return `${value.fullname.split("-"}`;
    }
    return value;
  },
});
hardy briar
#

i haven't tried advanced usage like this yet

#

let's see 🙂

hardy briar
#

this line is not valid fyi:
return ${value.fullname.split("-"};

mystic helm
hardy briar
#

and [item, modifier] should be [item, modifiers]

hardy briar
mystic helm
#

Thank you so much for this i actually understood what i did wrong

#

Basically i had to use a get instead of a set in order to do the thing which i was trying to do

#

And it still wouldnt have worked for this specific use case 🤣

#

So basically a set function is like a computed property which triggers on value change inside the component

#

And the get function is for initialization

hardy briar
#

get is called when the value is accessed

#

set is called when something attempts to change the value