如何在计算属性VUEjs中设置整个对象

时间:2017-10-03 07:50:21

标签: vue.js

我的问题是我有一个带有setter和getter的计算属性,如:

computed:{
        dataComputed:{
            get: function () {  return  this.dataProps},
            set: function (newValue) {
               //here my problem
                this.dataProps= Object.assign({}, newValue);
            }

        }
    },

但我的问题是我会分配从ajax调用中检索的整个对象,如:

var vm = this;
axios.post('/route', { data:this.dataComputed})
   .then(function (response) {
      vm.dataComputed = response.data.newData;
   }).catch(function (error) {});

但是在控制台中我有这个警告:

  

避免直接改变道具,因为该值将被覆盖   每当父组件重新渲染时。相反,使用数据或   基于prop值的计算属性。支持变异:   “dataProps”

如何将从后端检索的整个对象分配给计算对象?

1 个答案:

答案 0 :(得分:0)

根据您的错误消息,看起来dataProps是您的组件的属性,并且属性不能被突变。尝试将Object.assign({}, newValue);分配给data变量,而不是props中的变量。

相关问题