vuex 使用

cnpm install vuex --save

  

D:workspacexxxsrcmain.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

  

原文地址:https://www.cnblogs.com/tabCtrlShift/p/9163207.html