代码之家  ›  专栏  ›  技术社区  ›  Danie A

直接导入存储时,Vuex存储模块未按正确顺序加载

  •  0
  • Danie A  · 技术社区  · 7 年前

    我可能看不到明显的东西,但几个小时后我还是看不到。

    问题是:当我将Vuex存储导入非组件文件(api服务)时,一个模块正确加载,而另一个模块仅按名称加载,但在其他方面为空。

    // store.js
    import * as collections from "./modules/collections";
    import * as auth from "./modules/auth";
    
    export default new Vuex.Store({
      modules: {
        auth,
        collections
      }
    });
    

    这两个模块几乎完全相同。两者都有

    export const getters = {}
    export const actions = {}
    export const mutations = {}
    export const state = {}
    

    现在,当在其他一些非组件文件中 不要 包括商店,我的vue商店如下所示:

    {"auth":{"authenticated":false,"authToken":"123","loginPending":false,"loginError":{}},"collections":{"collectionsPending":false,"collectionsError":null,"collections":[]}}
    

    现在,当我导入商店以便在我的服务中使用它时,如下所示:

    import store from '../store'
    // NO OTHER MENTIONS OF STORE, ONLY IMPORTING
    

    突然,只有我的身份验证模块是“空的”

    {"auth":{},"collections":{"collectionsPending":false,"collectionsError":null,"collections":[]}}
    

    它与模块加载有关。

    添加加载顺序 console.log 声明:

    • 没有导入:

      INIT AUTH
      INIT collections
      INIT store
      
    • 导入时:

      INIT collections
      INIT store
      IMPORT STATEMENT -- INTO SERVICE
      INIT AUTH
      

    我正在使用Vue Webpack样板

    1 回复  |  直到 7 年前
        1
  •  0
  •   Danie A    7 年前

    唉,对不起。循环依赖确实如此。如果我那样做,我会得到警告,但没有。

    谢谢EmileBergeron。