Vuejs-基于其他计算属性的计算属性

时间:2019-10-08 08:04:17

标签: vue.js

我正试图从另一个计算属性中获取一个计算属性,就像这样:

var instance = new Vue({
    el: "#instance",
    data: {
        aha: ""
    },
    computed: {
        len: function(){
            return this.aha.length;
        },
        plus : function(){
            return this.len + 2;
        }
    }
});

这不起作用。尝试显示plus时,模板中显示“ NaN”。有办法使这项工作吗? this question的答案对我不起作用。

2 个答案:

答案 0 :(得分:2)

您正在尝试访问类型length的{​​{1}}字段。

number是数字,所以this.len未定义。您只需要使用this.len.length

this.len

答案 1 :(得分:0)

组件中的

data属性必须是一个函数,因此在您的情况下,应这样编写:

data () {
 return {
   aha: ""
 }
}
相关问题