脚本宝典收集整理的这篇文章主要介绍了Vue.js数据状态管理-Vuex,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
[TOC]
父子组件的通信
父 -> 子: PRops
子 -> 父: 自定义event
组件文档化
Vuex主要应用于中、大型单页应用的数据状态管理架构。
组件数据共享
父 -> 子:props
子 -> 父:自定义event
兄弟之间?其他非父子? :自定义event?
自定义event
VAR bus = new Vue()
// in component A's method
bus.$emIT('id-selected', 1)
// in component B's created hook
bus.$on('id-selected', function(id){
//...
})
共享数据源
const srcData = {
count: 0
}
const vmA = new Vue({
data: srcData
})
const vmB = new Vue({
data: srcData
})
谁在emit事件?谁在on事件?父组件和子组件强耦合
如何追踪数据的状态变化?
业务逻辑遍布各个组件,导致各种意想不到的问题
state
状态的容器
getters
状态的获取函数
mutations
修改状态的事件回调函数
actions
分发修改状态的事件
const Store = new Vuex.Store({
//state
state: {
count: 0
},
//mutations
mutations: {
INIT (state, data) {
state.count = data.count
},
INCREASE (state) {
state.count++
},
DECREASE (state) {
state.count--
}
},
//getters
getters: {
getCount (state) {
return state.count
}
},
//actions
actions: {
init(context){
context.COMmit('INIT', {
count: 10
})
},
inc(context){
context.commit('INCREASE')
},
dec(context){
context.commit('DECREASE')
}
}
})
数据的集合其中处理(DB)
数据的操作集中处理 (CRUD)
所有对数据的操作(CRUD)以请求(commit)的方式提交处理 (DAO)
以上是脚本宝典为你收集整理的Vue.js数据状态管理-Vuex全部内容,希望文章能够帮你解决Vue.js数据状态管理-Vuex所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。