cevio / super-vuex

:dress: It is a lightweight data stream distribution and storage model based on vuex
https://cevio.github.io/super-vuex/
47 stars 2 forks source link

您能给个完整的例子吗?请问子模块是必须的吗? #3

Closed 188007803 closed 6 years ago

188007803 commented 6 years ago

我在 store.js 里写

import { SuperVuex } from 'super-vuex';

export const Main = new SuperVuex('main');

Main.setState({
    name: 'test',
})

export default Main.init();

然后在 app.vue 里写

import store from './store'
export default {
     store,
     name: 'App,
     mounted(){
        // 怎么用? 以下都报错。
       this.$store.commit('name', 1)
       this.$store.commit('main.name', 1)
       this.$store.main.commit('name', 1)
    }
}
cevio commented 6 years ago

@188007803 在主模块中,其实我们省略了一个命名空间叫"Global",所以你的代码中

import store from './store'
export default {
     store,
     name: 'App',
     mounted(){
        // 怎么用? 以下都报错。
       this.$store.commit('name', 1)
       this.$store.commit('main.name', 1)
       this.$store.main.commit('name', 1)
    }
}

是取不到的(由于之前模式都是使用子模块),你应该这样写:

import store from './store'
export default {
     store,
     name: 'App',
     mounted(){
       this.$store.Global.commit('name', 1);
    }
}

在源码中定义的地方:https://github.com/cevio/super-vuex/blob/master/src/index.js#L9

export default class SuperVuex extends ChildVuex {
  constructor(name) {
    super(name || 'Global');
    this._pools = [];
    this.store = null;
    this.app = this;
  }
}

我们对程序进行了修正,https://github.com/cevio/super-vuex/commit/902f15a36b2b8d02f6ed2316909a2f7f5f28929a

不好意思,可能文档省略了对主模块的命名空间的说明,给您不便表示抱歉。感谢您的关注和使用!