1.引入mapState
import { mapState } from 'vuex'
2.通过mapState函数定义计算属性
computed: mapState({
// 箭头函数可使代码更简练
count: state => state.count,
// 传字符串参数 'count' 等同于 `state => state.count`
countAlias: 'count',
// 为了能够使用 `this` 获取局部状态,必须使用常规函数
countPlusLocalState (state) {
return state.count + this.localCount
}
}),
- 使用计算属性:
alert(this.countPlusLocalState);
其中,计算属性中用的的localCount需要在data函数进行定义