我正在使用Vue和Vuex进行中央存储管理.
解决方案是将对象从Vuex存储区克隆到本地数据对象,并将其绑定到表单,以防止在编辑时进行更新.我try 了所有可能的方法来克隆Vuex返回的可观察对象,但没有成功.
JSON.parse(JSON.stringify(obj))
和
Object.assign({}, vueObj)
和 also other deep cloning methods from external libraries like _ 和 jQuery.
这是我从Vuexstore 获得的对象:
If i stringify it, parse it 和 assign to a local vue data object it gets updated whenever the Vuex central storage is updated.
以下是我的代码(仅限组件,而非Vuexstore ):
<template>
<div class="">
<div v-if="localSelectedDataSource.id">
{{localSelectedDataSource.name}}
</div>
<div v-if="localSelectedDataSource.id">
<div><sui-input placeholder="Url" :value="localSelectedDataSource.url"/></div>
<div>{{localSelectedDataSource.method}}</div>
<div>{{localSelectedDataSource.pollingInterval}}</div>
</div>
<div class="datasource-list">
<div
v-bind:class="{ highlightdatasource: dataSource.view.highlighted }"
v-for="dataSource in dataSources"
v-on:mouseover="highlightDataSource(dataSource.id)"
v-on:mouseout="highlightDataSource(-1)"
v-on:click="editSelectedDataSourceLocal(dataSource.id)"
>
{{dataSource.name}} - {{dataSource.url}}
</div>
</div>
</div>
</template>
<script>
import {mapGetters} from 'vuex';
import {mapActions} from 'vuex';
export default {
name: 'DataSourceList',
data(){
return{
localSelectedDataSource: {}
}
},
computed: {
...mapGetters([
'dataSources',
'selectedDataSource'
])
},
methods: {
...mapActions([
'highlightDataSource',
'editSelectedDataSource'
]),
editSelectedDataSourceLocal: function(id){
this.editSelectedDataSource(id)
var t = JSON.parse(JSON.stringify(this.selectedDataSource))
if(this.localSelectedDataSource.id != this.selectedDataSource.id){
this.localSelectedDataSource = t
}
}
}
}
</script>
非常感谢.