Vuex
是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享
vuex:https://vuex.vuejs.org/zh/installation.html
使用Vuex统一管理好处
- 能够在Vuex中集中管理共享的数据,易于开发和后期维护
- 能够高效地实现组件之间的数据共享,提高开发效率
- 存储在Vuex中的数据都是响应式的,能够实时保持数据与页面的同步
vuex的安装
npm install vuex --save
vuex的使用
在src目录下新建vuex目录(或者store目录),在此文件中新建index.js文件
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
}
})
export default store
在src目录下的main.js文件中
import store from '@/vuex/index.js'
new Vue({
router,
store,
render: h => h(App),
}).$mount('#app')