1.在src文件夹中新建一个store文件夹,然后在里面建一个index.js文件
在index
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {}
const mutations = {}
const actions = {}
const getters = {}
export default new Vuex.Store({
state,
mutations,
actions,
getters
})
然后去main.js中
import store from './store'
注入//
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
然后给index.js中的
const mutations = {
setCartNum: (state, num) => {
state.cartNum = num
}
}
然后去商品详情页
import {mapMutations, mapGetters} from 'vuex'
然后在商品详情页中的methods
...mapMutations([
'setCartNum',
'getCartNum'
])
网友评论