【发布时间】:2019-05-19 12:32:24
【问题描述】:
我想从 props 传递值并将其用作 mapState 中的 命名空间,然后它显示错误:'初始化应用程序类型错误时出错:无法读取未定义的属性'存储'' 这是我的代码:
父组件:<Editor store="store-test" />
子组件:
export default {
props: ['store'],
computed: {
mapState(`${this.store}`, ['data'])
}
}
但是我替换了this.store = store-test,我收到了我想要的数据。
而且我不知道如何使用
...mapMutations({
function() {
}
})
是一样的
...mapState({
data(state) {
return state[this.store].data;
},
})
【问题讨论】:
-
当我使用:
...mapState('store-test', ['data'])时,我在 Vue-devtool 中收到:data: [],但是当将store-test替换为this.store时,它失败了 -
啊,这是您尝试设置的 命名空间。你不能这样做,因为
mapState在定义时返回它的值,但道具只在运行时存在。 -
在此处查看
mapMutationsAPI ~ vuex.vuejs.org/api/#mapmutations。仅供参考,您可以使用mapMutations({ saveData (commit) { ... } })