is this possible to pass parameter in computed properties in Vue.Js. I can see when having getters/setter using computed, they can take a parameter and assign it to a variable. like here from documentation:
// ...
computed: {
fullName: {
// getter
get: function () {
return this.firstName + ' ' + this.lastName
},
// setter
set: function (newValue) {
var names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[names.length - 1]
}
}
}
// ...
Is this also possible:
// ...
computed: {
fullName: function (salut) {
return salut + ' ' + this.firstName + ' ' + this.lastName
}
}
// ...
Where computed property takes a argument and returns desired output. However when I try this, I am getting this error:
vue.common.js:2250 Uncaught TypeError: fullName is not a function(…)
Should I be using methods for such cases?
However there are cases when you need to do so.I am going to show you a simple example passing value to computed property using getter and setter.
And the script
When the button clicked we are passing to computed property the name 'Roland' and in
set()
we are changing the name from 'John Doe' to 'Roland'.Below there is a common use case when computed is used with getter and setter. Say you have the follow vuex store:
And in your component you want to add
v-model
to an input but using the vuex store.