从js文件访问变异

时间:2019-01-09 18:21:40

标签: vuex

我正在尝试以编程方式从js文件中调用突变。我只是无法获得正确的语法,并且不断收到诸如“未定义bar”之类的ReferenceErrors。

我有以下设置:


//substore.js
export default {
  namespaced: true,
  state: {
    foo:[]
  },
  mutations:{
    bar(state) {
      //...
    }
  },
};

//store.js

import substore from '/pathTo/substore.js';

Vue.use(Vuex);
export default new Vuex.Store({ 
  modules :{
    substore
  }
});

//someFile.js

import store from 'pathTo/store.js';

someMethod(){
  console.dir(store); 
  // Shows all the store data. The relevant data is:
  //   _mutations is an Object within store
  //      substore/bar is an Array[1] within the Object
  //        0: wrappedMutationHandler() length:1 is inside the Array

  store._mutations.substore.bar(); // Can't get this to work
  store._mutations.substore/bar(); // Can't get this to work either
  store._mutations['substore/bar']; // Can't get this to work either
}

对于它的价值,如果我使用组件,它可以按预期工作。

import {mapMutations} from 'vuex';
export default {
  methods: {
    ...mapMutations({bar:'substore/bar'}),
    baz(){
      this.bar();
    }
  },
};

0 个答案:

没有答案