vuex的基本操作

1.安装vuex依赖包

npm install vuex  --save

2.导入vuex包

import  vuex  from  ‘vuex’

Vue.use(Vuex)

3.创建store对象

复制代码
const store=new Vuex.Store({

// state 中存放的就是全局共享的数据

state :{count:0}

})
复制代码

state中存放的就是全局共享的数据,咱们在这里提供一个count,值是0,现在创建出来的store和vue没有任何关联

 

4.将store挂载到vue实例中

复制代码
new Vue({

  el:'#app',

  render: h=>h(app),

  router,

  // 将创建的共享数据,挂载到 vue 实例中

  // 所有的组件,就可以直接从 store 中获取全局的数据了

  store

})
原文地址:https://www.cnblogs.com/-zzq/p/13031812.html