我在Vuex开了一家店,店里空荡荡的
const state = {
data: {
}
};和一个简单的突变测试,以改变值或添加新数据
const mutations = {
changeOrAdd(state, {key,value}) {
state.data[key] = value;
}
};如果我提交了一个changeOrAdd,它会被添加到状态,但它不具有反应性。
我使用了一个简单的技巧来更改默认值
const state = {
data: {
change: 0
}
};在变异中:
const mutations = {
changeValueAttr(state, {
key,
value
}) {
state.data[key] = value;
state.data.change++;
}
};每次我改变或添加一个新的值,它看起来就像是一个反应性。
但是,有没有一种方法可以在没有“默认”变量和这个愚蠢的技巧的情况下做到这一点呢?
在store vue中添加新数据并使其具有反应性?
谢谢
发布于 2019-02-22 00:28:46
由于键最初并未在数据中声明,因此Vue无法跟踪更改。您需要使用Vue.set来动态添加属性,请参见change detection caveats
import Vue from 'vue'
const mutations = {
changeOrAdd(state, {key, value}) {
Vue.set(state.data, key, value)
}
}https://stackoverflow.com/questions/54811786
复制相似问题