javascript - Can I pass parameters in computed properties in Vue.Js -
is possible pass parameter in computed properties in vue.js. can see when having getters/setter using computed, can take parameter , assign variable. here 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 possible:
// ... computed: { fullname: function (salut) { return salut + ' ' + this.firstname + ' ' + this.lastname } } // ...
where computed property takes argument , returns desired output. when try this, getting error:
vue.common.js:2250 uncaught typeerror: fullname not function(…)
should using methods such cases?
you should use methods:
<span>{{ fullname('hi') }}</span> methods: { fullname: function (salut) { return salut + ' ' + this.firstname + ' ' + this.lastname } }
p.s. difference between computed property , method computed properties cached , change when dependencies change - that's why can't pass parameters there. methods evaluated every time call them.
Comments
Post a Comment