0
点赞
收藏
分享

微信扫一扫

Vue官方文档(49): vuex中mapState函数的用法


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
}
}),

  1. 使用计算属性:

alert(this.countPlusLocalState);

其中,计算属性中用的的localCount需要在data函数进行定义


举报

相关推荐

Vuex中mutations的用法

0 条评论