vuex这一块的代码
let store = new Vue.Store({
state: {
totalPrice: 0
},
getters: {
getTotal (state) {
return state.totalPrice
},
// mutations只能同步的请求
mutations: {
increment (state, price) {
state.totalPrice += price
},
decrement (state,price) {
state.totalPrice -= price
}
},
// actions可以异步的请求
actions: {
increase (context, price) {
context.commit('increment', price)
}
}
})
2.其他去vuex中的getTotal数据
export default {
computed: {
totalPrice() {
return this.$store.getters.getTotal
}
}
}
|-- index.html
|-- main.js
|-- api
|-- components
| |-- App.vue
| |_ ....
|__ store
|-- index.js
|-- actions.js
|-- mutations.js
|__ modules
|-- cart.js
|-- products.js