vue中如何computed对象属性值呢?

data(){
    return {
        a : {
            b: "测试",
            c: 10,
            d: 20,
            e: 20,
            f: 40
        }
    }
},
computed:{
    "a.b": function(){
        return this.a.c + this.a.d + this.a.e + this.a.f;
    }
}

请问下,如果对一个对象的属性进行监控呢?如上写法,当对象属性c、d、e、f变化时无法触发计算,要怎么写才对呢?

阅读 20.7k
3 个回答
computed:{
    computedA: function(){
        return {...this.a,b:this.a.c + this.a.d + this.a.e + this.a.f};
    }
}

计算属性不需要再data里声明,用的时候直接 this.b 就可以了

computed:{
    b: function(){
        return this.a.c + this.a.d + this.a.e + this.a.f;
    }
}
推荐问题