Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 指定要在组件中使用的Vuex存储_Vue.js_Vue Component_Vuex_Vuex Modules - Fatal编程技术网

Vue.js 指定要在组件中使用的Vuex存储

Vue.js 指定要在组件中使用的Vuex存储,vue.js,vue-component,vuex,vuex-modules,Vue.js,Vue Component,Vuex,Vuex Modules,我已将Vuex存储模块化,需要子组件能够访问数据/使用与其父组件相同的存储 以下是我尝试过的: 从“@/components/services/child components/ChildComponent”导入ChildComponent; 从“vuex”导入{mapActions,mapState,mapGetters}; 导出默认值{ 组成部分:{ “子组件”:子组件, }, 计算:{ …mapState(“服务/机会”{ statusListOpportunities:“statusL

我已将Vuex存储模块化,需要子组件能够访问数据/使用与其父组件相同的存储

以下是我尝试过的:


从“@/components/services/child components/ChildComponent”导入ChildComponent;
从“vuex”导入{mapActions,mapState,mapGetters};
导出默认值{
组成部分:{
“子组件”:子组件,
},
计算:{
…mapState(“服务/机会”{
statusListOpportunities:“statusListOpportunities”,
}),
}
}

从“vuex”导入{mapActions,mapState,mapGetters};
导出默认值{
道具:{
vuexStore:{
类型:字符串,
},
},
计算:{
…mapState(this.vuexStore{
statusListOpportunities:“statusListOpportunities”,
}),
}
}
这是我得到的错误

未捕获的TypeError:无法读取未定义的属性“vuexStore”

我一直在传递道具,但是道具的杂耍越来越混乱,如果我能指定一个组件应该使用哪个存储,那就容易多了

我也无法直接在子组件中指定存储,因为ParentTwo.vue可能使用不同的存储,例如
服务/案例


任何人都知道为什么这行不通。我也愿意接受其他解决方案。

查看下面演示中的函数=
myMapState
,用法=
…mapState(this.vuexStore,…)
遇到了错误的上下文问题(
不是组件实例)

一种解决方案是直接访问computed property函数中的this.$store.state,就像computed property=
test2
在下面的演示中所做的那样

Vue.config.productionTip=false
让myMapState=函数(选项,测试){
!测试和控制台。错误(测试)
返回Vuex.mapState(选项)
}
const store=新的Vuex.store({
声明:{
主题:“光”,
好:是的
}
})
Vue.组件(“v测试”{
模板:`1:{{test1}}:{{test2}}

2:{{theme1}}

`, 道具:['vuexStore'], 计算:{ …myMapState({'test1':this.vuexStore,'theme1':'theme'},this.vuexStore), 测试2:函数(){ 返回此.store.state[此.vuexStore] } } }) 新Vue({ el:“#应用程序”, 商店 })

…映射状态(this.vuexStore…)
它应该遇到错误的上下文问题