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,现在创建出来的storevue没有任何关联

 

4.store挂载到vue实例中

new Vue({

  el:'#app',

  render: h=>h(app),

  router,

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

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

  store

})

猜你喜欢

转载自www.cnblogs.com/ranyihang/p/12914103.html
今日推荐