vuex
Vuex中核心概念
Mutation用于变更Store中的数据
只能通过Mutation来变更store数据,不可直接操作Store中的数据
第一个参数默认是state,第二个参数是传递过来的数据
mutations: { getInfoList(state, list){ state.list = list state.amount = list.length }, }
action不能直接修改state中数据,只能是提交给mutation,通过他来修改,
action可以异步,mutation里面只能同步修改。
action函数中第一个参数默认是context,是整个store对象。
reqGetInfoList(context){ axios.get('/list.json').then(res =>{ const {data} = res context.commit('getInfoList', data) context.commit('handlenav', 'all') }) } }
getter 对state进行加工处理,本身并不修改state中的数据。
getters: { showNum(state){ return state.list.filter(item=>!item.done).length } }
vuex是一个专门为vue应用程序设计的状态管理模式,它采用集中式应用程序组建的状态。
state:应用驱动的数据源
action:用户输入的改变通过提交给mutation修改state
vuex中的映射方法: mapAction,mapMutation,mapState,mapGetter等
解决了父子组件之间传递的繁琐,适用于中大型单页面应用