vuex 使用

cnpm install vuex --save

  

D:\workspace\xxx\src\main.js

import Vuex from 'vuex'

Vue.use(Vuex)

let store = new Vuex({
  state: {
    totalPrice: 0
  },
  mutations: {
    increment (state, price) {
      state.totalPrice += price
    },
    decrement (state, price) {
      state.totalPrice -= price
    }
  }
})

  

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})

  

使用

this.$store.state.totalPrice

  

猜你喜欢

转载自www.cnblogs.com/tabCtrlShift/p/9163207.html