Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Vuejs-基于其他计算属性的计算属性_Javascript_Vue.js - Fatal编程技术网

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

Javascript Vuejs-基于其他计算属性的计算属性,javascript,vue.js,Javascript,Vue.js,我试图从另一个计算属性中获取一个计算属性,如下所示: var instance = new Vue({ el: "#instance", data: { aha: "" }, computed: { len: function(){ return this.aha.length; }, plus : function(){

我试图从另一个计算属性中获取一个计算属性,如下所示:

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

这不管用。当我试图显示
plus
时,我在模板中得到
NaN
。有没有办法让这一切顺利进行?的答案对我不起作用。

您正在尝试访问类型为
number
length
字段

this.len
是数字,因此
this.len.length
未定义。您只需使用
this.len

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

组件中的
数据
属性必须是一个函数,因此在您的情况下,它应该这样编写:

data () {
 return {
   aha: ""
 }
}

正确的。愚蠢的错误。非常感谢。