javascript - 在 Vuex 中使用带有 mapState、mapMutation 的 get/set Computed 属性
问题描述
我使用名为的 Vuex 模块具有以下计算属性main
:
computed: {
foo: {
get(){
return this.$store.state.main.foo;
},
set(value) {
this.$store.commit("main/foo", value);
}
}
}
我想使用get
/set
模式,因为我想使用v-model="foo"
. 不得不$store
直接交谈是非常冗长的。有没有更简单的方法使用mapState
,mapMutation
甚至createNamespacedHelpers
?
解决方案
我建议您尝试vuex-map-fields模块,它包含一个mapFields
帮助方法,可帮助您动态设置 getter 和 setter。
推荐阅读
- jquery - 单击时哈希更改
- php - 错误:无法连接到 MySQL。调试错误:2002 调试错误:连接被拒绝
- android - 无法在 Android Oreo 中触发广播接收器
- angular - 如何从角度的电话号码中找到国家代码?
- mysql - 如何从第三个关系表中获取总和
- android - 如何检查设备是否刚刚连接到互联网
- unit-testing - 在角度 6 中运行 ng 测试期间出现 null 的“appendchild”错误
- java - 将 Lucene 与 H2 数据库一起使用时出错
- android - 我可以在 oreo 注册接收器 BluetoothAdapter.ACTION_STATE_CHANGED 吗?
- typo3 - TYPO3 9.5 LTS routeEnhancers for news with uid 使路径独一无二