最佳答案
我有一个 vuex商店,如下:
import spreeApi from '../../gateways/spree-api'
// initial state
const state = {
products: [],
categories: []
}
// mutations
const mutations = {
SET_PRODUCTS: (state, response) => {
state.products = response.data.products
commit('SET_CATEGORIES')
},
SET_CATEGORIES: (state) => {
state.categories = state.products.map(function(product) { return product.category})
}
}
const actions = {
FETCH_PRODUCTS: (state, filters) => {
return spreeApi.get('products').then(response => state.commit('SET_PRODUCTS', response))
}
}
export default {
state,
mutations,
actions
}
我想称之为突变: SET_CATEGORIES
,来自于突变: SET_PRODUCTS
,但是这给了我一个错误:
Js: 22未捕获(在承诺中) ReferenceError: commit 不定义(...)
什么应该是正确的方法来做到这一点。我尝试了 store.commit
和 this.commit
,但这些也给类似的错误。