【问题标题】:in vue-cli vuex , use this.$store can't get store object, but use this.$store.store it works在 vue-cli vuex 中,使用 this.$store 无法获取 store 对象,但使用 this.$store.store 可以
【发布时间】:2017-10-11 04:46:27
【问题描述】:

【问题讨论】:

    标签: vuejs2 vuex


    【解决方案1】:

    这是由export default 引起的。尝试直接导出const。

    例子:

    store.js

    import 'Vue' from 'vue';
    import 'Vuex' from 'vuex';
    
    Vue.use(Vuex);
    
    export const store = new Vuex.Store({
      state: {
         //your code here//
      }
    });
    

    Main.js

    import 'store' from 'wherever the file is/mystore.js';
    
    new Vue({
      el: '#app',
      store, <-- Here is defined store to use along the my app
      render: h => h(App)
    });
    

    【讨论】:

    • 成功了,非常感谢你的帮助,解决了我几天的困惑,但是我仍然很困惑,当我使用export default时会出错,如果必须使用用export default然后怎么写
    • 明白了,如果我用export { store }就可以了,我应该仔细阅读es6模块,再次感谢你给我一个方向,
    猜你喜欢
    • 2023-03-18
    • 2019-12-29
    • 2019-04-05
    • 2021-07-06
    • 2018-08-01
    • 2019-02-02
    • 2018-11-23
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多