2017-05-14 84 views
3

我需要調用vuex模塊它不起作用。我已經看過文檔,但仍然無效。希望可以有人幫幫我。Vuex模塊突變

const stateLocations ={ 

    state: { 

     provinces: {}, 
     cities: {} 
    }, 
    mutations: { 
     provinces(state, payload){ 

      state.provinces = payload 
     } 
    } 
} 


const store = new Vuex.Store({ 

    modules: { 
     locations: stateLocations 
    } 


}) 

我的代碼來調用突變

created(){ 

      var store = this.$store 

      axios.get('api/provinces') 
       .then(function(response){ 

        store.state.locations.commit('provinces', response.data) 
       }) 
       .catch() 
     } 

這一個不工作store.state.locations.commit('provinces', response.data) TY

回答

7

因爲你沒有在模塊中實現namespaced,你只需要

this.$store.commit('provinces', response.data) 

如果要啓用命名空間:

const stateLocations = { 
    namespaced: true, 
    state: { 
    ... 

那麼你提交的突變是這樣的:

this.$store.commit('locations/provinces', response.data) 
+0

由於它的工作原理就像一個魅力... – Rbex