vuex 中关于mapGetters 的作用 - 博客园
文章推薦指數: 80 %
mapGetters 工具函数会将store 中的getter 映射到局部计算属性中。
它的功能和mapState 非常类似,我们来直接看它的实现: mapGetters 的实现也 ...
首页
新闻
博问
专区
闪存
班级
我的博客
我的园子
账号设置
简洁模式...
退出登录
注册
登录
每天都要进步一点点
善始者实繁,克终者盖寡。
技术栈:angular+ionic+vue+mint-ui+react-native+teaset+h5plus+mui+nodejs+webpack+gulp+ES6+vux+typescript2+weex+weex-ui+微信小程序+react+ant-design+ant-design-mobile+material-ui+nervjs+taro+taro-ui+springboot
vuex中关于mapGetters的作用
mapGetters工具函数会将store中的getter映射到局部计算属性中。
它的功能和mapState非常类似,我们来直接看它的实现:
exportfunctionmapGetters(getters){
constres={}
normalizeMap(getters).forEach(({key,val})=>{
res[key]=functionmappedGetter(){
if(!(valinthis.$store.getters)){
console.error(`[vuex]unknowngetter:${val}`)
}
returnthis.$store.getters[val]
}
})
returnres
}
mapGetters的实现也和mapState很类似,不同的是它的val不能是函数,只能是一个字符串,而且会检查valinthis.$store.getters的值,如果为false会输出一条错误日志。
为了更直观地理解,我们来看一个简单的例子:
import{mapGetters}from'vuex'
exportdefault{
//...
computed:{
//使用对象扩展操作符把getter混入到computed中
...mapGetters([
'doneTodosCount',
'anotherGetter',
//...
])
}
}
经过mapGetters函数调用后的结果,如下所示:
import{mapGetters}from'vuex'
exportdefault{
//...
computed:{
doneTodosCount(){
returnthis.$store.getters['doneTodosCount']
},
anotherGetter(){
returnthis.$store.getters['anotherGetter']
}
}
}
再看一个参数mapGetters参数是对象的例子:
computed:mapGetters({
//映射this.doneCount到store.getters.doneTodosCount
doneCount:'doneTodosCount'
})
经过mapGetters函数调用后的结果,如下所示:
computed:{
doneCount(){
returnthis.$store.getters['doneTodosCount']
}
}
.
posted@
2017-10-0722:38
每天都要进步一点点
阅读(8798)
评论(0)
编辑
收藏
举报
刷新评论刷新页面返回顶部
Copyright©2022每天都要进步一点点
Poweredby.NET6onKubernetes
延伸文章資訊
- 1vuex里mapState,mapGetters使用详解- 云+社区- 腾讯云
一、基本用法. 1. 初始化并创建一个项目 vue init webpack-simple vuex-demo cd vuex-demo npm install. 2. 安装vuex npm i...
- 2vuex全解說-- mapState, getters帶參數, mapGetters, 展開運算符...
mapGetters 輔助函數與前面提到的 mapState 用法相近,可以簡化程式碼。
- 3vuex 中輔助函數mapGetters的基本用法詳解 - WalkonNet
mapGetters 輔助函數. mapGetters 輔助函數僅僅是將store 中的getter 映射到局部計算屬性:. 1、在組件或界面中不使用mapGetter調用映射vuex中的 ...
- 4Day29 Vuex Getter用法介紹 - iT 邦幫忙
Day29 Vuex Getter用法介紹 ... mapGetters 與mapState 用法相近 ... </div> </template> <script> import { mapG...
- 5[Vue] Vuex 是什麼? 怎麼用? — Getters (3/5) | by itsems - Medium
... 把api 抓回來的值丟進state 裡面: 然後在computed 的mapGetters 加上剛剛的getteers returnFn ,並在methods 裡面寫上template ...