vue3.0透傳屬性和事件的使用方式舉例
如何“透傳屬性和事件”
父組件在使用子組件的時候,如何“透傳屬性和事件”給子組件呢?
- 透傳屬性和事件并沒有在子組件中用
props
和emits
聲明 - 透傳屬性和事件最常見的如
@click
和class
、id
、style
- 當(dāng)子組件只有一個根元素時,透傳屬性和事件會自動添加到該根元素上;如果根元素已有
class
或style
屬性,它會自動合并
組合式API測試 :
父組件App.vue
<script setup> import ChipVue from './components/Chip.vue'; function say() { alert('Hello') } </script> <template> <!-- 透傳的屬性(style,class,title)在子組件中并沒有在 props 聲明 --> <!-- 透傳的事件(click)在子組件中并沒有在 emits 聲明 --> <ChipVue class="rounded" style="border: 1px solid blue;" title="紙片" @click="say" /> </template>
子組件 Chip.vue
<template> <!-- 當(dāng)子組件只有一個根元素時,透傳屬性和事件會自動添加到該根元素上 如果根元素已有 class 或 style 屬性,它會自動合并 --> <button class="chip" style="box-shadow: 0 0 8px grey;"> 普通紙片 </button> </template> <style> .chip { border: none; background-color: rgb(231, 231, 231); padding: 8px 15px; } .rounded { border-radius: 100px; } </style>
渲染結(jié)果:
如何禁止“透傳屬性和事件”
- 當(dāng)子組件只有一個根元素時,透傳屬性和事件會自動添加到該根元素上,那怎么阻止呢?
- 在選項式 API 中,你可以在組件選項中設(shè)置
inheritAttrs: false
來阻止; - 在組合式 API 的
<script setup>
中,你需要一個額外的<script>
塊來書寫inheritAttrs: false
選項聲明來禁止
組合式API測試 :
父組件App.vue
<script setup> import ChipVue from './components/Chip.vue'; function say() { alert('Hello') } </script> <template> <!-- 透傳的屬性(style,class,title)在子組件中并沒有在 props 聲明 --> <!-- 透傳的事件(click)在子組件中并沒有在 emits 聲明 --> <ChipVue class="rounded" style="border: 1px solid blue;" title="紙片" @click="say" /> </template>
子組件 Chip.vue
<script> export default { inheritAttrs: false // 阻止自動透傳給唯一的根組件 } </script> <!-- 在組合式 API 的 <script setup> 中, 你需要一個額外的 <script> 塊來書寫 inheritAttrs: false 選項聲明來禁止 --> <script setup></script> <template> <!-- 當(dāng)子組件只有一個根元素時,透傳屬性和事件會自動添加到該根元素上 如果根元素已有 class 或 style 屬性,它會自動合并 --> <button class="chip" style="box-shadow: 0 0 8px grey;"> 普通紙片 </button> </template> <style> .chip { border: none; background-color: rgb(231, 231, 231); padding: 8px 15px; } .rounded { border-radius: 100px; } </style>
多根元素的“透傳屬性和事件”
多根節(jié)點(diǎn)的組件并沒有自動“透傳屬性和事件”的行為,由于Vue
不確定要將“透傳屬性和事件”透傳到哪里,所以我們需要v-bind="$attrs"
來顯式綁定,否則將會拋出一個運(yùn)行時警告。
組合式API測試:
父組件App.vue
<script setup> import ChipVue from './components/Chip.vue'; function say() { alert('Hello') } </script> <template> <!-- 透傳的屬性(style,class,title)在子組件中并沒有在 props 聲明 --> <!-- 透傳的事件(click)在子組件中并沒有在 emits 聲明 --> <ChipVue class="rounded" style="border: 1px solid blue;" title="紙片" @click="say" /> </template>
子組件Chip.vue
<template> <!-- 多根節(jié)點(diǎn)的組件并沒有自動“透傳屬性和事件”的行為 --> <button class="chip"> 普通紙片 </button> <hr> <button class="chip" v-bind="$attrs"> 普通紙片 </button> <hr> <button class="chip" v-bind="$attrs"> 普通紙片 </button> </template> <style> .chip { border: none; background-color: rgb(231, 231, 231); padding: 8px 15px; } .rounded { border-radius: 100px; } </style>
訪問“透傳屬性和事件”
- 在選項式 API 中,我們可通過
this.$attrs
來訪問“透傳屬性和事件” - 在組合式 API 中的
<script setup>
中引入useAttrs()
來訪問一個組件的“透傳屬性和事件”
組合式API測試:
父組件App.vue
<script setup> import ChipVue from './components/Chip.vue'; function say() { alert('Hello') } </script> <template> <!-- 透傳的屬性(style,class,title)在子組件中并沒有在 props 聲明 --> <!-- 透傳的事件(click)在子組件中并沒有在 emits 聲明 --> <ChipVue class="rounded" style="border: 1px solid blue;" title="紙片" @click="say" /> </template>
子組件Chip.vue
<script setup> import { useAttrs } from 'vue'; // 透傳的屬性和事件對象 let attrs = useAttrs() // 在 JS 中訪問透傳的屬性和事件 function showAttrs() { console.log(attrs) console.log(attrs.class) console.log(attrs.title) console.log(attrs.style) attrs.onClick() } </script> <template> <button class="chip" v-bind="attrs"> 普通紙片 </button> <hr> <h6>{{ attrs }}</h6> <ul> <li>{{ attrs.title }}</li> <li>{{ attrs.class }}</li> <li>{{ attrs.style }}</li> </ul> <button @click="attrs.onClick()">執(zhí)行透傳的事件</button> <hr> <button @click="showAttrs">在 JS 中訪問透傳的屬性和事件</button> </template> <style> .chip { border: none; background-color: rgb(231, 231, 231); padding: 8px 15px; margin: 10px; } .rounded { border-radius: 100px; } </style>
注意:
- 雖然這里的
attrs
對象總是反映為最新的“透傳屬性和事件”,但它并不是響應(yīng)式的 (考慮到性能因素),你不能通過偵聽器去監(jiān)聽它的變化- 如果你需要響應(yīng)性,可以使用
prop
或者你也可以使用onUpdated()
使得在每次更新時結(jié)合最新的attrs
執(zhí)行副作用
總結(jié)
到此這篇關(guān)于vue3.0透傳屬性和事件使用的文章就介紹到這了,更多相關(guān)vue3.0透傳屬性和事件使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用Vue Elements實現(xiàn)文件預(yù)覽功能
在現(xiàn)代 web 開發(fā)中,用戶與系統(tǒng)的交互體驗越來越重要,而文件上傳和文件預(yù)覽是最常見的交互場景之一,本文將詳細(xì)介紹如何在 Vue 項目中使用 Vue Elements 來實現(xiàn)文件預(yù)覽的功能,包括基本使用方法、常見實例、性能優(yōu)化以及樣式自定義等內(nèi)容,需要的朋友可以參考下2025-01-01webpack轉(zhuǎn)vite的詳細(xì)操作流程與問題總結(jié)
Vite是新一代的前端開發(fā)與構(gòu)建工具,相比于傳統(tǒng)的webpack,Vite 有著極速的本地項目啟動速度(通常不超過5s)以及極速的熱更新速度(幾乎無感知),下面這篇文章主要給大家介紹了關(guān)于webpack轉(zhuǎn)vite的詳細(xì)操作流程與問題總結(jié)的相關(guān)資料,需要的朋友可以參考下2023-03-03vue雙向數(shù)據(jù)綁定指令v-model的用法
這篇文章主要介紹了vue雙向數(shù)據(jù)綁定指令v-model的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08vue路由事件beforeRouteLeave及組件內(nèi)定時器的清除方法
今天小編就為大家分享一篇vue路由事件beforeRouteLeave及組件內(nèi)定時器的清除方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue-cli項目使用mock數(shù)據(jù)的方法(借助express)
現(xiàn)如今前后端分離開發(fā)越來越普遍,前端人員寫好頁面后可以自己模擬一些數(shù)據(jù)進(jìn)行代碼測試,這樣就不必等后端接口,提高了我們開發(fā)效率。今天就來分析下前端常用的mock數(shù)據(jù)的方式是如何實現(xiàn)的,需要的朋友可以參考下2019-04-04