我想从 props 传递值并将其用作mapState 中的命名空间,然后它显示错误:'初始化应用程序时出错 TypeError:无法读取未定义的属性'store' 这里我的代码:
父组件: <Editor store="store-test" />
子组件:
export default {
props: ['store'],
computed: {
mapState(`${this.store}`, ['data'])
}
}
Run Code Online (Sandbox Code Playgroud)
但是我替换了this.store = store-test,我收到了我想要的数据。我不知道如何使用
...mapMutations({
function() {
}
})
Run Code Online (Sandbox Code Playgroud)
是同样的
...mapState({
data(state) {
return state[this.store].data;
},
})
Run Code Online (Sandbox Code Playgroud)
因为在编译时mapState为您的computed定义返回其值,所以您不能使用props它们,因为它们仅在运行时分配。
你将不得不使用
computed: {
data () {
return this.$store.state[this.store].data
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要,您仍然可以使用mapState但不能将 prop 值用作命名空间字符串
mapState({
data1 (state) { // note: cannot be an arrow function
return state[this.store].data1
},
data2 (state) {
return state[this.store].data2
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1390 次 |
| 最近记录: |