Vuex是用来实现 *组件之间数据的共享的* , 是实现组件全局状态(数据)管理的一种机制。
Vuex什么时候用?需要大范围,频繁的使用数据共享时使用到。
Vuex的基本使用(若用脚手架自动解决以下步骤)1.安装vuex依赖包: npm install vuex --save
2.导入vuex包:
import Vuex from 'vuex'
Vue.use(Vuex)
3.创建store对象
const store = new Vuex.store({
state : { count: 0 } //用于存放全局共享数据
})
4.将store对象挂在到vue实例中
new Vue({
el:'#app',
render: h => h(app),
router,
store
})