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 }
    }
 )

4.将store对象挂载到vue实例中

new Vue({
  el: '#app',
  render: h => h(app),
  router,
  //将创建的共享数据对象挂载到vue实例中
  //所有的组件都能直接从store中获取全局的数据了
  store
})
微信扫一扫体验微信小程序
Last modification:September 21, 2022
If you think my article is useful to you, please feel free to appreciate