Vuex状态管理模式

index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 100
  },
  mutations: {

  },
  actions: {

  },
  getters: {

  },
  modules: {

  }
})

export default store

main.js
引入store

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'

Vue.config.productionTip = false

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

vue

<p>{{$store.state.count}}</p>
<button type="button" @click="$store.state.count++">+</button>
<button type="button" @click="$store.state.count--">-</button>

posted @ 2021-07-14 10:28  thomas_blog  阅读(30)  评论(0编辑  收藏  举报