Vue的状态管理和vuex库
今天我学习了Vue的状态管理和vuex库。Vue的状态管理可以让我们管理整个应用的状态,在不同的组件中共享数据和方法。vuex是Vue官方提供的状态管理库,提供了store、state、mutations、actions四个核心概念:
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment: function(state) {
state.count++
}
},
actions: {
increment: function(context) {
context.commit('increment')
}
}
})
var app = new Vue({
el: '#app',
store
})
然后在各个组件中使用vuex提供的辅助函数把store中的数据和方法映射到组件的属性中:
Vuex.mapState(['count'])
Vuex.mapMutations(['increment'])
Vuex.mapActions(['increment'])
明天我将继续学习Vue的过渡动画和动态组件。