当多次获取state中的数据时会出现大量的 $store.state.属性名 造成代码的冗余同时也很麻烦。就可使用 mapState(引用时使用的名称,‘state中的属性名’) 或 mapState(‘state中的属性名’)
第二种需要 引用时使用的名称 与 state中的属性名 相同
例如
//引入
import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'
computed{
//借助mapState生成计算属性 从state中获取数据(对象写法)
...mapState({Sum:'sum',Subject:'subject',……}),
//借助mapState生成计算属性 从state中获取数据(数组写法) 当引用时使用的名称与state中的属性名相同时
...mapState(['school',……]),
}
//mapGetters
<h1>{{Sum}}</h1>
<h1>{{school}}</h1>
<h1>{{Sbuject}}</h1>
mapGetters与mapState用法相同只不过是mapGetters获取的是getters中的数据
computed{
...mapGetters(['bigSum'])
}
<h3>{{bigSum}}</h3>
mapMutations生成与Mutations对话的方法用法与mapGetters相同,只不过数据必须通过调用时传输
mapActions生成与Actions对话的方法用法与mapGetters相同,只不过数据必须通过调用时传输