[英]Vuejs - computed property based on other computed property
I'm trying to get a computed property from another computed property, like so:我正在尝试从另一个计算属性中获取计算属性,如下所示:
var instance = new Vue({
el: "#instance",
data: {
aha: ""
},
computed: {
len: function(){
return this.aha.length;
},
plus : function(){
return this.len + 2;
}
}
});
This doesn't work.这行不通。 I get
NaN
in my template when I try to display plus
.当我尝试显示
plus
时,我的模板中出现了NaN
。 Is there a way to make this work?有没有办法使这项工作? The answer to this question doesn't work for me.
这个问题的答案对我不起作用。
You are trying to access the length
field of type number
.您正在尝试访问
number
类型的length
字段。
this.len
is number, so this.len.length
is undefined. this.len
是数字,所以this.len.length
是未定义的。 you just need to use this.len
:你只需要使用
this.len
:
var instance = new Vue({
el: "#instance",
data: {
aha: ""
},
computed: {
len: function(){
return this.aha.length;
},
plus : function(){
return this.len+ 2;
}
}
});
data
property in component must be a function, so in your case it should be written like this:组件中的
data
属性必须是 function,所以在你的情况下它应该这样写:
data () {
return {
aha: ""
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.