vue学习4-VueX

npm install vuex --save

vuex 是定义的一个大仓库,来存放项目中用到的数据,解决组件之间传值的复杂性。

mutations 是官方的修改仓库中的值的唯一方法

 1 // The Vue build version to load with the `import` command
 2 // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
 3 import Vue from 'vue'
 4 import App from './App'
 5 import router from './router'
 6 import VueX from 'vuex'
 7 
 8 Vue.use(VueX)
 9 //定义一个大仓库,来存放项目中用到的数据
10 const store = new VueX.Store({
11   state:{
12     NoteList1:['吃饭','睡觉','打豆豆']
13   },
14   mutations:{
15     //修改仓库中的值的唯一方法
16     AddNote:function (state,note) {
17       state.NoteList1.push(note);
18     }
19   }
20 })
21 Vue.config.productionTip = false
22 /* eslint-disable no-new */
23 new Vue({
24   el: '#app',
25   router,
26   store:store,//将上面定义的大仓库与实例联系在一起
27   components: { App },
28   template: '<App/>'
29 })
原文地址:https://www.cnblogs.com/hexintong/p/10130444.html