vue的辅助函数有:1、mapState,将全局状态管理的state值映射到组件的计算属性;2、mapGetters,将全局状态管理的getters值映射到组件的计算属性;3、mapActions;4、mapMutations。
本教程操作环境:windows7系统、vue2.9.6版,DELL G3电脑。
1.Vue的辅助函数
mapState、mapGetters、mapActions、mapMutations
mapState 将全局状态管理的state值映射到组件的计算属性
mapGetters 将全局状态管理的getters值映射到组件的计算属性
mapMutation 将mutation的值映射到方法里
mapActions 将actions里的值映射到方法里
2.mapState 辅助函数
mapState
是什么?官方的解释是:
当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键
当初在看到这个解释的时候可能觉得非常空洞,难以理解。生成计算属性是什么?少按几次键???
mapState是state的语法糖,什么是语法糖?我对语法糖的理解就是,我明明已经对一种操作很熟练了,并且这种操作也不存在什么问题,为什么要用所谓的"更好,更好的操作"?,用了一段时间后,真香!
3.如何使用
在使用mapState之前,要导入这个辅助函数。
import { mapState } from 'vuex'
使用方式
{{count}} {{dataCount}}<script> import { mapState } from &#39;vuex&#39; export default { data () { return { str: '国籍', dataCount: this.$store.state.count } }, computed: mapState({ count: 'count', // 第一种写法 sex: (state) => state.sex, // 第二种写法 from: function (state) { // 用普通函数this指向vue实例,要注意 return this.str + ':' + state.from }, // 注意下面的写法看起来和上面相同,事实上箭头函数的this指针并没有指向vue实例,因此不要滥用箭头函数 // from: (state) => this.str + ':' + state.from myCmpted: function () { // 这里不需要state,测试一下computed的原有用法 return '测试' + this.str } }), methods: { increment () { this.$store.commit('increment') }, decrement () { this.$store.commit('decrement') } } } </script>{{sex}}{{from}}{{myCmpted}}
computed
可以接收mapState
函数的返回值,你可以用代码中的三种方式去接收store中
的值,具体可以看注释。
如果我在后面想使用mapState怎么办?其实很简单
//之前的computed computed:{ fn1(){ return ...}, fn2(){ return ...}, fn3(){ return ...} ........ } //引入mapState辅助函数之后 computed:mapState({ //先复制粘贴 fn1(){ return ...}, fn2(){ return ...}, fn3(){ return ...} ...... //再维护vuex count:'count' })
4.对象展开运算符
…mapState并不是mapState的扩展,而是…对象展开符的扩展 。
//之前的computed computed:{ fn1(){ return ...}, fn2(){ return ...}, fn3(){ return ...} ........ } //引入mapState辅助函数之后 computed:{ //原来的继续保留 fn1(){ return ...}, fn2(){ return ...}, fn3(){ return ...} ...... //再维护vuex ...mapState({ //这里的...不是省略号了,是对象扩展符 count:'count' })
mapState
、mapGetters
、mapActions
、mapMutations
的使用方法大同小异。。。
【相关推荐:《vue.js教程》】
以上是vue的辅助函数有哪些的详细内容。更多信息请关注PHP中文网其他相关文章!

使用类选择器和ID选择器取决于具体用例:1)类选择器适用于多元素、可重用样式,2)ID选择器适用于唯一元素、特定样式。类选择器更灵活,ID选择器处理速度更快但可能影响代码维护性。

keykeygoalsandmotivationsbehindhtml5weretoenhancesemantstructure,Improvemultimediasupport,andensureBetterperformanceandCompatibalityAcroscaroscaroscaroscarossdecrossdecrossdecrossdecrossdecrossdecrossdecrossdevices,drivendybytheneedtoAddresshtml4'slimitationsand limitiTations and limittations andmeetmeetModerntructAndmmoderntructss.1)

IDSareNiqueAndusedForsingLelement,andleclassEsareReusableFormultPirultElements.1)useIdIdSforuniqueElementsLikeAspeCificheader.2)useclassesforconsistentSistentSistentStyActStyAcroSsmultipleLementslike.3)becautiouswithspecificitificitieAsideCerrrase.4)

html5aimstoenhancewebaccctible,互动性和效率。1)ITSupportsMultimediawithOutPlugins,Simplifyinginguserexperience.2)Semanticmarkmarksmarkupimprovissupimprovessupstructureandacccessessible.3)增强bacegencementingIncrassubility.4)

html5isnotparticulllydifficulttousebutrequirequireSustingingItsFeatures.1)smanticelementslike like ,,,和iMproveructure,andimprovucture,可读性,seo和acctibility.2)多中性倍增量,且可读性

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互动,可及可访问。1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒体综合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中