<vuex第二弹>vuex之Getter(前端网备份)

所有的结构都参考上一弹
Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性)。
index.js
//vue 2.0官方建议在computed里面的不使用箭头函数,因为箭头函数的this 指向上一层,而function的this指向本层
const getters={
count:function(state){
return state.count+=100
}
}
export default new Vuex.Store({
state,
mutations,
getters
})
vue
import { mapState,mapMutations,mapGetters } from 'vuex'
然后主要是写法的区别
这里主要有2种写法的区别,未注释的是用了mapGetters的简写,注意vue的cpmputed的结构以及以后在这里的map之前都得加...
computed:{
...mapState([
'count'
]),
...mapGetters([
'count'
])
// count(){
// return this.$store.getters.count;
// }
},

posted @ 2019-06-10 13:40  影思密达ing  阅读(126)  评论(0编辑  收藏  举报