为什么我在vuex存储中的初始状态未定义?

时间:2018-07-13 09:27:57

标签: javascript vue.js vuejs2 vuex

始终使用vuex-typescript并获得未定义的初始状态。一旦我重置状态,它就可以工作,但是仅当我重置和刷新窗口时。这是一些简单模块的设置:

import Vue from "vue";
import Vuex from "vuex";

import { module } from "./some_module";

let store = new Vuex.Store({
  modules: {
    module,

  },
  plugins: [persist({
    namespace: "mainnamespace",
    initialState: {
    },
    expires: 7 * 24 * 60 * 60 * 1e3 // 1 week
  })
  ]
});

some_module.ts

export interface State {
  something: any;
}

const state: State = {
  something: [],
};

export const module = {
namespaced: true,
  getters: {

    getSomethingArray: (state: State) => {

      return state.something;
    },

  },
  mutations: { 

    resetState: (s: State) => {
      const initial = state;
      Object.keys(initial).forEach(key => { s[key] = initial[key]; });
    },
  }
  actions: {///}
}

const { commit, read, dispatch } =
  getStoreAccessors<HistoryState, any>("somemodule");
const mutations = module.mutations;
const getters = module.getters;
const actions = module.actions;
export const getSomeStuffFromHere = read(getters.getSomethingArray);

当我运行应用程序并且console.log(getSomethingArray(this.$store))我得到undefined时,当我console.log(this.$store)时我可以看到somemodule nemespace,但是其状态不是something: []而是{{1 }}

1 个答案:

答案 0 :(得分:0)

这是有线的,已通过添加状态进行了修复:

public MyConfigurator() {
    serviceLocator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    ServiceLocatorUtilities.enableImmediateScope(serviceLocator);
    ServiceLocatorUtilities.bind(serviceLocator, new AbstractBinder() {

        @Override
        protected void configure() {
            bind(MySocket.class).to(MySocket.class);
            bind(LManager.class).to(DCService.class);

        }
    });
}

@Override
public <T> T getEndpointInstance(Class<T> endpointClass) {
    return serviceLocator.getService(endpointClass);
}
}
相关问题