Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用
sync
在vue2.4以前,父組件向子組件傳值用props;子組件不能直接更改父組件傳入的值,需要通過$emit觸發(fā)自定義事件,通知父組件改變后的值。比較繁瑣,寫法如下:
//父組件 <template> <div class="parent"> <p>父組件傳入子組件的值:{{name}}</p> <fieldset> <legend>子組件</legend> <child :val="name" @update="modify"> </child> </fieldset> </div> </template> <script> import Child from './Child' export default { components:{Child}, data () { return { name:'linda' } }, methods:{ modify(newVal){ this.name=newVal } } } </script> //子組件 <template> <label class="child"> 輸入框: <input :value=val @input="$emit('update',$event.target.value)"/> </label> </template> <script> export default { props:['val'] } </script>
vue2.4以后的寫法明顯舒服許多,上面同樣的功能,直接上代碼
//父組件 <template> <div class="parent"> <p>父組件傳入子組件的值:{{name}}</p> <fieldset> <legend>子組件</legend> <child :val.sync="name"> </child> </fieldset> </div> </template> <script> import Child from './Child' export default { components:{Child}, data () { return { name:'linda' } } } </script> //子組件 <template> <label class="child"> 輸入框: <input :value=val @input="$emit('update:val',$event.target.value)"/> </label> </template> <script> export default { props:['val'] } </script>
寫法上簡化了一部分,很明顯父組件不用再定義方法檢測值變化了。其實只是對以前的$emit方式的一種縮寫,.sync其實就是在父組件定義了一update:val方法,來監(jiān)聽子組件修改值的事件。
$attrs
想象一下,你打算封裝一個自定義input組件——MyInput,需要從父組件傳入type,placeholder,title等多個html元素的原生屬性。此時你的MyInput組件props如下:
props:['type','placeholder','title',...]
很丑陋不是嗎?$attrs專門為了解決這種問題而誕生,這個屬性允許你在使用自定義組件時更像是使用原生html元素。比如:
//父組件 <my-input placeholder="請輸入你的姓名" type="text" title="姓名" v-model="name"/>
my-input的使用方式就像原生的input一樣。而MyInput并沒有設(shè)置props,如下
<template> <div> <label>輸入框:</label><input v-bind="$attrsAll" @input="$emit('input',$event.target.value)"/> </div> </template> <script> export default { inheritAttrs:false, computed: { $attrsAll() { return { value: this.$vnode.data.model.value, ...this.$attrs } } } } </script>
基礎(chǔ)掃盲
v-model是v-bind:value和v-on:input的簡寫,所以在父組件你完全可以直接寫 :value="name",@input="val => name = val"。查看文檔
疑難
引用下vue的官方api中對$attrs的說明
$attrs包含了父作用域中不作為 prop 被識別 (且獲取) 的特性綁定 (class 和 style 除外)
比較迷惑的一點是給子組件設(shè)置:value="name"相當于給子組件設(shè)置props:['value'],所以在MyInput中直接從$attrs獲取不到value,需要重新包裝$attrsAll,添加value屬性。所以子組件還有下面寫法,我傾向于這種寫法,因為它更優(yōu)雅
<template> <div> <label>輸入框:</label><input v-bind="$attrs" :value="value" @input="$emit('input',$event.target.value)"/> </div> </template> <script> export default { inheritAttrs:false, props:['value'] } </script>
$listener
同上面$attrs屬性一樣,這個屬性也是為了在自定義組件中使用原生事件而產(chǎn)生的。比如要讓前面的MyInput組件實現(xiàn)focus事件,直接這么寫是沒用的
<my-input @focus="focus" placeholder="請輸入你的姓名" type="text" title="姓名" v-model="name"/>
必須要讓focus事件作用于MyInput組件的input元素上,最終的MyInput源碼如下:
<template> <div> <label>輸入框:</label><input v-bind="$attrsAll" v-on="$listenserAll"/> </div> </template> <script> export default { inheritAttrs:false, props:['value'], computed:{ $attrsAll() { return { value: this.value, ...this.$attrs } }, $listenserAll(){ return Object.assign( {}, this.$listeners, {input:(event) => this.$emit('input',event.target.value)}) } } } </script>
到此這篇關(guān)于Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用的文章就介紹到這了,更多相關(guān)Vue2.4 .sync、$attrs、$listeners內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue v2.4中新增的$attrs及$listeners屬性使用教程
- Vue 父子組件數(shù)據(jù)傳遞的四種方式( inheritAttrs + $attrs + $listeners)
- Vue為什么要謹慎使用$attrs與$listeners
- Vue組件通信$attrs、$listeners實現(xiàn)原理解析
- 詳解Vue中$props、$attrs和$listeners的使用方法
- Vue封裝組件利器之$attrs、$listeners的使用
- Vue中$attrs與$listeners的使用教程
- vue $attrs和$listeners的使用與區(qū)別
- 簡單聊聊vue2.x的$attrs和$listeners
相關(guān)文章
使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)
這篇文章主要介紹了使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù),整篇文章圍繞Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)的想換自來哦展開內(nèi)容,需要的小伙伴可以參考一下2021-10-10ant design vue中table表格滾動加載實現(xiàn)思路
在處理一寫數(shù)據(jù)量特別大的情況下,我們不能把后端的數(shù)據(jù)一次性全部拿到前端在table表格中展示,為了考慮性能優(yōu)化,使用了滾動加載表格數(shù)據(jù),這篇文章主要介紹了ant design vue中table表格滾動加載實現(xiàn)思路,需要的朋友可以參考下2024-07-07Vuex管理dialog、toast等常見全局性組件狀態(tài)時唯一性的問題解決
工作中經(jīng)常會用到類似于?dialog、toast、popover?等一些狀態(tài)提示組件,這篇文章主要介紹了Vuex管理dialog、toast等常見全局性組件狀態(tài)時唯一性的問題,需要的朋友可以參考下2022-11-11非Vuex實現(xiàn)的登錄狀態(tài)判斷封裝實例代碼
這篇文章主要給大家介紹了關(guān)于非Vuex實現(xiàn)的登錄狀態(tài)判斷封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02