辅助函数

mapState和mapMutation的基本使用方法

传递的是 this.$store.state 属性, 这样就可以省去 this.$store 了
mapState传递的是 this.$store.commit 属性,这样就可以省去 this.$store 了
mapMutations传递的是 this.$store.commit 属性,这样就可以省去 this.$store 了

import{mapState}from'vuex'exportdefault{computed: {// 日常写法account() {returnthis.$store.state.account},password() {returnthis.$store.state.password},age() {returnthis.$store.state.age},other:() =>"other"},computed: {// 子模块的属性因有了命名空间 无法直接使用数组 magic// mutations 是没有的 可以看下文...mapState({userName:state=>state.user.name,userAge:state=>state.user.age})// magic style1...mapState(['account','password','age']),other:() =>"other"},computed: {// magic style2 自定义属性法名...mapState({account:'account',password:'password',age:'age', }),other:() =>"other"},computed: {// magic style3 更多灵活处理...mapState({account:(state) =>{// account: state => state.account | account(state) {} returnstate.account},password:(state) =>{// password: state => state.age | password(state) {}returnstate.password},age:(state) =>{// age: state => state.age | age(state) {} returnstate.age} }),other:() =>"other"} }

2个语句功能相等
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容