在本地存储道具(vue2)

Storing props locally (vue2)

以下:

我有:

https://codesandbox.io/s/u3mr8

给出以下警告:

(这个想法是为了避免 mutating 道具)。简单的复制对象操作会产生什么副作用?我不明白。该函数只是将 props 保存到数据中。

拖放失败:

你真的需要 setter 计算道具吗?

正在查看:

我想出了:

https://codesandbox.io/s/39sgo

太棒了,没有警告,没有错误;只是组件不再渲染(无法从 prop 获取保存的数据)。

任何 ideas/suggestions/help/advice 都会非常非常棒。

我认为抛出错误是因为不允许在 getter 中设置生成计算 属性 的值。在得到计算结果的同时修改初始值是一个逻辑循环。相反,您可以 return 初始调用 getter 的道具值(如果尚未设置本地值)。

  get() {
    if (!this.itemSectionPropsLocal["itemSectionCategory"]) {
      return Object.assign({}, this.itemSectionProps)[
        "itemSectionCategory"
      ];
    }
    return this.itemSectionPropsLocal["itemSectionCategory"];
  },
  set(value) {
    this.itemSectionPropsLocal = value;
  },

此外,在 setter 中,您应该分配接收到的值而不是 prop。如果你想在挂载后 prop 值发生变化时更新本地值,你应该使用观察者。

watch: {
  itemSectionProps: {
    deep: true,
    handler(val){
      this.getPropsLocal = Object.assign({}, val["itemSectionCategory"])
    }
  }
}