vue3.0透?jìng)鲗傩院褪录氖褂梅绞脚e例
如何“透?jìng)鲗傩院褪录?rdquo;
父組件在使用子組件的時(shí)候,如何“透?jìng)鲗傩院褪录?rdquo;給子組件呢?
- 透?jìng)鲗傩院褪录]有在子組件中用
props和emits聲明 - 透?jìng)鲗傩院褪录畛R姷娜?code>@click和
class、id、style - 當(dāng)子組件只有一個(gè)根元素時(shí),透?jìng)鲗傩院褪录?huì)自動(dòng)添加到該根元素上;如果根元素已有
class或style屬性,它會(huì)自動(dòng)合并
組合式API測(cè)試 :
父組件App.vue
<script setup>
import ChipVue from './components/Chip.vue';
function say() {
alert('Hello')
}
</script>
<template>
<!-- 透?jìng)鞯膶傩裕╯tyle,class,title)在子組件中并沒有在 props 聲明 -->
<!-- 透?jìng)鞯氖录╟lick)在子組件中并沒有在 emits 聲明 -->
<ChipVue
class="rounded"
style="border: 1px solid blue;"
title="紙片"
@click="say"
/>
</template>子組件 Chip.vue
<template>
<!--
當(dāng)子組件只有一個(gè)根元素時(shí),透?jìng)鲗傩院褪录?huì)自動(dòng)添加到該根元素上
如果根元素已有 class 或 style 屬性,它會(huì)自動(dòng)合并
-->
<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é)果:

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

