简体   繁体   English

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

[英]why is my initial state in vuex store undefined?

Using vuex-typescript and getting inital state undefined all the time. 一直使用vuex-typescript并不确定初始状态。 Once i reset state it is working but only when i reset and window refresh. 一旦我重置状态,它就可以工作,但是仅当我重置和刷新窗口时。 Here is my setup for some simple module: 这是一些简单模块的设置:

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 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);

when i run app and console.log(getSomethingArray(this.$store)) i got undefined and when i console.log(this.$store) i can see somemodule nemespace but its state is not something: [] but some __ob__: Observer 当我运行应用程序和console.log(getSomethingArray(this.$store))undefined ;当我console.log(this.$store)我可以看到一些somemodule nemespace但其状态不是something: []而是some __ob__: Observer

This is wired got it fixed with adding state: 这是通过添加状态来解决的:

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

    getSomethingArray: (state: State) => {

      return state.something;
    },

  },
  mutations: { 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM