这是否可能在Vue.Js的计算属性中传递参数.我可以看到当getter / setter使用calculate时,他们可以使用一个参数并将其分配给一个变量.喜欢这里从
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] } } } // ...
这也是可能的:
// ... computed: { fullName: function (salut) { return salut + ' ' + this.firstName + ' ' + this.lastName } } // ...
其中计算属性接受参数并返回所需的输出.但是,当我尝试这个,我得到这个错误:
vue.common.js:2250 Uncaught TypeError: fullName is not a function(…)
我应该用这种方法吗?
解决方法
你应该使用方法:
<span :text="fullName('Hi')"></span> methods: { fullName: function (salut) { return salut + ' ' + this.firstName + ' ' + this.lastName } }
附:计算属性和方法之间的区别是计算属性被缓存,只有在依赖关系发生变化时才会更改,这就是为什么不能在其中传递参数的原因.每次打电话时都会评估方法.