如何重用不同商店的Vuex状态属性?

发布于 2025-02-09 21:18:19 字数 1007 浏览 1 评论 0原文

我正在尝试重复使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2025-02-16 21:18:19

考虑到每个商店都需要拥有自己的isloading状态属性,可以使用助手功能来完成:

const withIsLoading = storeConfig => deepMerge({}, storeConfig, {
  state: {
    isLoading: false,
    isLoaded: false,
  },
  mutations: {
    setIsLoading(state, isLoading) {...},
    setIsLoaded(state, isLoaded) {...},
  },
});

合并

export default withIsLoading({
  namespaced: true,
})

deepmerge是任何深层合并实现,例如lodash 。如果有多个帮助者,例如withisloading(withsomethingelse({...}})),它们可以与lodash flow/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:

const withIsLoading = storeConfig => deepMerge({}, storeConfig, {
  state: {
    isLoading: false,
    isLoaded: false,
  },
  mutations: {
    setIsLoading(state, isLoading) {...},
    setIsLoaded(state, isLoaded) {...},
  },
});

and

export default withIsLoading({
  namespaced: true,
})

Where deepMerge is any deep merge implementation, e.g. Lodash merge. In case there are multiple helpers like withIsLoading(withSomethingElse({... })), they can be composed with Lodash flow/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.

一梦浮鱼 2025-02-16 21:18:19

加载和加载状态您将用于副作用。因此,您可以采取行动。并且不要访问状态直接使读取值读取值和突变以设置值。
你会在下面做

someAction ({ dispatch, commit, getters, rootGetters }) {
            getters.someGetter // -> 'foo/someGetter'
            rootGetters.someGetter // -> 'someGetter'
            rootGetters['bar/someGetter'] // -> 'bar/someGetter'
   
    
            commit('someMutation') // -> 'foo/someMutation'
            commit('someMutation', null, { root: true }) // -> 'someMutation'
          },

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

someAction ({ dispatch, commit, getters, rootGetters }) {
            getters.someGetter // -> 'foo/someGetter'
            rootGetters.someGetter // -> 'someGetter'
            rootGetters['bar/someGetter'] // -> 'bar/someGetter'
   
    
            commit('someMutation') // -> 'foo/someMutation'
            commit('someMutation', null, { root: true }) // -> 'someMutation'
          },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文