如何重用不同商店的Vuex状态属性?
我正在尝试重复使用iSloading
跨多家商店的状态属性。我希望它可以通过一个包含状态和突变的外部对象来起作用,当我将它们导入某个商店时,该商店将自动与状态合并,并在此对象中包含突变。与生成新商店的模块不同,我希望将此对象与导入的商店合并。
例如:
// load-mixin.js
export default {
state: {
isLoading: false,
isLoaded: false,
},
mutations: {
setIsLoading(state, isLoading) {
state.isLoading = isLoading
},
setIsLoaded(state, isLoaded) {
state.isLoaded = isLoaded
},
}
}
// store1.js
import loadState from 'load-mixin'
export default {
namespaced: true,
// this should have the states and mutation from `load-mixin`
mixins: [
loadState
],
}
// store2.js
import loadState from 'load-mixin'
export default {
namespaced: true,
// this should have the states and mutation from `load-mixin`
mixins: [
loadState
],
}
但是看起来Vuex中没有混音。有没有实现这一目标的方法?
I'm trying to reuse the isLoading
state property across multiple stores. I expect it to work by having one external object containing states and mutations, and when I import them into some store, this store will automatically be merged with the states and mutation contained in this object. Different from modules that generate a new store, I expect this object to be merged with the store that imports it.
Ex:
// load-mixin.js
export default {
state: {
isLoading: false,
isLoaded: false,
},
mutations: {
setIsLoading(state, isLoading) {
state.isLoading = isLoading
},
setIsLoaded(state, isLoaded) {
state.isLoaded = isLoaded
},
}
}
// store1.js
import loadState from 'load-mixin'
export default {
namespaced: true,
// this should have the states and mutation from `load-mixin`
mixins: [
loadState
],
}
// store2.js
import loadState from 'load-mixin'
export default {
namespaced: true,
// this should have the states and mutation from `load-mixin`
mixins: [
loadState
],
}
But looks like there aren't mixins in Vuex. Is there Vuex way of achieving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑到每个商店都需要拥有自己的
isloading
状态属性,可以使用助手功能来完成:合并
deepmerge
是任何深层合并实现,例如lodash。如果有多个帮助者,例如
withisloading(withsomethingelse({...}}))
,它们可以与lodashflow
/code>/flow> flow
或类似的功能,以避免嵌套。请注意,Vuex Store状态可以定义为工厂功能,而不是对象,这通常是一个好习惯。对于更通用的商店定义,需要考虑这一点,该定义不限于指定为
state
的普通对象。Vuex插件也可以包含可以在商店之间重复使用的逻辑。
Considering that each store needs to have its own
isLoading
state property, this can be done with helper function:and
Where
deepMerge
is any deep merge implementation, e.g. Lodashmerge
. In case there are multiple helpers likewithIsLoading(withSomethingElse({... }))
, they can be composed with Lodashflow
/flowRight
or a similar function to avoid nesting.Notice that Vuex store state can be defined as factory function instead of an object, which is generally a good practice. This needs to be considered for a more universal store definition that isn't limited to plain objects specified as
state
.Also a Vuex plugin can contain logic that can be reused between stores.
加载和加载状态您将用于副作用。因此,您可以采取行动。并且不要访问状态直接使读取值读取值和突变以设置值。
你会在下面做
Loading and loaded state you will used for side-effects. so you can get this in actions. And dont access state directly make getters for read value and mutation for setting value.
you will do by below