nah*_*hoj 9 vue.js vuex vuejs2
如何将数据从 vuex 状态克隆到本地数据属性?
状态
this.tsStore.shemes
数据属性
data () {
return { shemes: [] }
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试在更新的 () 中执行此操作,this.shemes = this.tsStore.shemes但它似乎还剩下一个绑定。不要在变异处理程序之外改变 vuex 存储状态”。
我需要克隆状态并做任何我需要对这些数据做的事情,同时不要影响我的状态。
小智 21
尝试
this.shemes = JSON.parse ( JSON.stringify ( this.tsStore.shemes) )
Run Code Online (Sandbox Code Playgroud)
这将从存储中的数组中克隆所有值和对象。
小智 5
您需要创建一个新数组。this.tsStore.shemes 为您提供对绑定数组的引用。您可以尝试使用扩展运算符或 arr.slice() 创建具有相同内容的新数组。请注意,这是一个浅拷贝。
this.shemes = [...this.tsStore.shemes]
Run Code Online (Sandbox Code Playgroud)
或者
this.shemes = this.tsStore.shemes.slice()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13421 次 |
| 最近记录: |