vue.js - 如何保持 vue 计算属性与 vuex 状态相同,反之亦然?
问题描述
我想保持 vue 计算属性与 vuex 状态相同,反之亦然。但它没有达到我的预期结果。这是一个简单的小提琴来展示我的问题。 https://jsfiddle.net/qtttttttttting/Lteoxz9f/22/
const store = new Vuex.Store({
state: {
fullName: "hhh hhh"
},
mutations: {
change (state,data) {
state.fullName = data;
}
}
})
new Vue({
el: "#app",
data(){return {}},
methods: {
toggle: function(){
console.log(333)
this.fullName="qqq ttt";
console.log(this.fullName)
}
},
computed: {
fullName: {
// getter
get: function () {
console.log(111);
return store.state.fullName;
},
// setter
set: function (newValue) {
console.log(222);
store.commit("change", this.fullName);
}
}
},
watch:{
fullName(){
console.log(444);
store.commit("change", this.fullName);
}
}
})
解决方案
您的计算设置器中有错字: https ://jsfiddle.net/0o2cnrvf/
set: function (newValue) {
console.log(222);
store.commit("change", newValue);
}
推荐阅读
- android - 为 arm32 编译 Python 3.6.5 静态二进制文件失败
- angularjs - 显示日期时间的angularjs过滤器/指令在当前日期时间之前多少
- ios - 水平滚动collectionview时如何确定选择了哪个tableView Cell
- java - Java HashMap.put 用“new”关键字抛出 NullPointerException
- javascript - 尝试使用选择/选项标签显示不同类型的图表
- python - 使用 rcParams 为 matplotlib 设置全局“legend.loc”?
- html - Angular 2按钮类型提交不起作用
- python - 图像处理:向量化 numpy 数组元素替换
- angular - 发生错误 InvalidPipeArgument Angular 6
- react-native - RN Webview 未从项目级文件夹加载图像