【发布时间】:2019-10-22 00:24:36
【问题描述】:
当我更改文本字段的值时,我为此组件设置的深度观察器不会更新存储。我找不到正确更改存储对象(配置文件)键/值对的方法(组名:string)
Profile.vue 元素:
<v-text-field v-model="profileData.groupName" label="Group Name"></v-text-field>
Profile.vue JS:
import { mapGetters, mapMutations } from "vuex";
export default {
name: "Profile",
created() {
this.profileData = JSON.parse(JSON.stringify(this.getProfile()));
console.log(this.profileData);
},
data() {
return {};
},
methods: {
...mapGetters(["getProfile"]),
...mapMutations(["setProfile"])
},
watch: {
profileData: {
handler(value) {
this.setProfile(value);
},
deep: true
}
}
};
build.js(store.js 的模块):
const state = {
profile: {
"groupName": "Happy group",
"groupNumber": "9999999999",
"groupContact": "Bob Ross"
}
};
const getters = {
getProfile: (state) => state.profile,
};
const actions = { };
const mutations = {
setProfile: (state, profile) => (state.profile = profile)
};
export default {
state,
getters,
actions,
mutations,
}
我不确定为什么状态没有更新。有人知道吗?
感谢您的阅读
【问题讨论】:
-
(1) 分配一个对象不会复制它,所以
pp实际上是指您存储中的同一个对象(并且您应该在编辑时仍然看到 Vuex 突变警告)。 (2) 在pp上尝试一个深度观察者,用新值调用setProfile。
标签: javascript vue.js vuex store