代码之家  ›  专栏  ›  技术社区  ›  dendog

动态填充Vuex状态

  •  0
  • dendog  · 技术社区  · 7 年前

    我需要根据 data 根组件中的变量。

    应用程序。vue:

    export default {
      store : store,
      name: 'app',
      data() {
        clientId : 1  
      }
    }
    

    百货商店js:

    export const store = new Vuex.Store({
      state : {
        clientConfig : clientConfig
    });
    

    以及 clientConfig 将存储在单独的文件中。

    基于对初始Vuejs应用程序的一些输入,实现Vuex状态的动态/编程加载的最佳方法是什么?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   ittus    7 年前

    以下是1种可能的解决方案:

    export const store = new Vuex.Store({
      state : {
        clientConfigs :{
          "1": clientConfig1,
          "2": clientConfig2
        },
        clientId: "1"
      },
      getters: {
        currentClientConfig: state => {
          return state.clientConfigs[state.clientId]
        }
      },
      mutations: {
       setClientId (state, newValue) {
         state.clientId = newValue
       }
      }
    });
    

    在您的组件中,您可以通过以下方式访问配置 this.$store.getters.currentClientConfig

    要更新 clientId ,可以使用突变。在组件中,可以使用 this.$store.commit('setClientId', yourNewClientId)