【发布时间】:2018-05-07 21:13:05
【问题描述】:
将 Vuex 本地计算对象与 get/set 以及存储映射结合使用时出现语法错误。
正如在 Vuex 文档中看到的,您可以像这样使用对象传播操作符映射您的存储方法,例如:
computed: {
localComputed () { /* ... */ },
// mix this into the outer object with the object spread operator
...mapState({
// ...
})
}
https://vuex.vuejs.org/en/state.html##object-spread-operator
您还可以创建计算设置器,例如:
computed: {
fullName: {
// getter
get: function () {
return this.firstName + ' ' + this.lastName
},
// setter
set: function (newValue) {
var names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[names.length - 1]
}
}
}
https://vuejs.org/v2/guide/computed.html#Computed-Setter
我可以使用 get set 或 mapState/mapGetters 等创建计算对象 - 但不能组合使用。它破坏了语法(错误是:函数声明后的预期函数名)。
computed: {
localComputed () {
localMethod: {
get: function () {
// retrieve
},
set: function (newContent) {
// set
}
}
}, ...mapState([
]), ...mapGetters([])
}
如何将这两者结合起来?
【问题讨论】:
标签: javascript vue.js vuex