vuex入門---周肥肥建

1134 ワード

1.vuexプラグインのインストール
npm install vuex --save

2.vuexのロード
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

3.storeを作成する(2つのパラメータ、2つのメソッドを含む)
const store = new Vuex.Store({
  state: {
    count: 0,
    student:[{id:0,name:'zhu1',age:19,sex:'boy'},
      {id:1,name:'zhu1',age:19,sex:'boy'},
      {id:2,name:'zhu1',age:19,sex:'boy'},]
  },
  mutations: {
    add (state) {
      state.count++
    },
    sub (state) {
      state.count--
    }
  }

4.storeのエクスポート
export default  store;

5.main.jsでstoreをロード
import store from "./store/index.js";

6.new vueメソッドにロードされたstoreを追加するには、次のようにします.
new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app')

7.コンポーネントで呼び出す.ケース

    

{{$store.state.count}}

{{this.$store.state.student[0].name}}