【问题标题】:How set entire object in computed property VUEjs如何在计算属性 VUEjs 中设置整个对象
【发布时间】:2018-03-14 08:35:57
【问题描述】:

我的问题是我有一个带有 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,因为值会被覆盖 每当父组件重新渲染时。相反,使用数据或 基于道具值的计算属性。道具被变异: “数据属性”

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

【问题讨论】:

    标签: vue.js


    【解决方案1】:

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

    【讨论】:

    • 你好,我不知道是不是最好的办法,我已经解决了创建一个数据{ object:dataProps } 并在 computed: get: function () { return this.object}, set: function (newValue){ this.object= newValue; }
    • 嗯,就是这样;-)
    猜你喜欢
    • 2020-10-03
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2018-09-19
    • 1970-01-01
    • 2017-08-08
    相关资源
    最近更新 更多