Javascript 具有多个模块的vuex命名空间映射状态
我一定错过了什么。如何将vuex mapState与多个模块一起使用 据了解,除了将对象作为参数传递外,命名空间mapState还可以接受两个参数:命名空间和表示模块成员的对象名称数组。像这样Javascript 具有多个模块的vuex命名空间映射状态,javascript,module,namespaces,vuex,Javascript,Module,Namespaces,Vuex,我一定错过了什么。如何将vuex mapState与多个模块一起使用 据了解,除了将对象作为参数传递外,命名空间mapState还可以接受两个参数:命名空间和表示模块成员的对象名称数组。像这样 // an imcomplete vue export default { computed: mapState('user', ['addresses', 'creditCards']) }; 但是,如果我想将第二个命名空间中的对象添加到computed,该怎么办?e、 g.类似这样的供应商:
// an imcomplete vue
export default {
computed: mapState('user', ['addresses', 'creditCards'])
};
但是,如果我想将第二个命名空间中的对象添加到computed,该怎么办?e、 g.类似这样的供应商:
mapState('vendor', ['products', 'ratings'])
let userMapState = mapState('user', ['addresses', 'creditCards']);
let vendorMapState = mapState ('vendor', ['products', 'ratings']);
let mergedMapStates = Object.assign({}, userMapState, vendorMapState);
目前我正在合并两个mapState,如下所示:
mapState('vendor', ['products', 'ratings'])
let userMapState = mapState('user', ['addresses', 'creditCards']);
let vendorMapState = mapState ('vendor', ['products', 'ratings']);
let mergedMapStates = Object.assign({}, userMapState, vendorMapState);
然后:
// an imcomplete vue
export default {
computed: mergedMapStates
};
这是可行的,但这不是正确的方法。还是它?使用:
这来自vuex文档,您可以在一个
…mapState({})
中完成所有操作
编辑2019
您还可以传递嵌套模块的路径,并使模块引用更干净(谢谢)
您也可以像这样使用Object.assign。与您当前的解决方案类似,但更干净一点
computed: Object.assign(
mapState('user', ['addresses', 'creditCards']),
mapState('vendor', ['products', 'ratings']
)
使用扩展运算符访问多个模块状态值
…映射状态('user'{
isLoggedIn:({activeUser})=>!!activeUser?.id
}),
…mapState('测验',['getCategories']))
如果没有太多的名称空间,可以尝试以下方法:
...mapState({
userAddresses: 'user/addresses',
userCreditCards: 'user/creditCards'
vendorProducts: 'vendor/products',
vendorRatings: 'vendor/ratings',
})
我把这三个点误解为文件中的省略号。我还不习惯这个播报员,真管用!谢谢是的,但是如何从组件中引用地址?@MichealJ.Roberts与任何其他数据一样,例如
{{{addresses}
或
文档(现在)明确指出,这种方法“可能会有点冗长”,因此下面解释的替代方法是“将模块命名空间字符串作为第一个参数传递给帮助程序,以便使用该模块作为上下文完成所有绑定”
...mapState({
userAddresses: 'user/addresses',
userCreditCards: 'user/creditCards'
vendorProducts: 'vendor/products',
vendorRatings: 'vendor/ratings',
})