一、情景说明
这个getter
是对store
中state
里面的变量进一步加工处理的结果。
类似于vc
中的computed
属性
那么,大家就会问,那为什么不直接用computed
了?
这个vuex
中的getter
有个好处就是,它里面的getter
结果,可以被任何一个vc
组件使用。
可以简单的理解为全局的computed
。
二、案例
index.js
配置getters
//准备state 用于存储数据
const state = {
sum: 0 //当前的和
};
//准备getters——用于加工state中的数据
const getters = {
bigSum(state){
return state.sum*10
}
}
//创建并暴露store
export default new Vuex.Store({
actions,
mutations,
state,
getters
});
vc
中使用getter
<h1>当前求和放大10倍为:{{$store.getters.bigSum}}</h1>