vue3中的透傳attributes教程示例詳解
引言
最近兩年都是在使用 react 進(jìn)行項(xiàng)目開發(fā),看技術(shù)博客都是針對(duì) react 和 javaScript 高級(jí)方面的,對(duì) vue 的知識(shí)基本上遺忘的差不多了。最近開始慢慢回顧 vue 的知識(shí)以及對(duì)新的語法進(jìn)行學(xué)習(xí),為后面的計(jì)劃做準(zhǔn)備(哈哈哈,懂得都懂)。
先從最簡單的樣式開始吧。
綁定樣式
vue 的樣式綁定要人性化很多,react 實(shí)現(xiàn) vue 的這種寫法,還需要專門下載一個(gè)第三方庫: classnames。
vue 的樣式綁定有兩種形式:對(duì)象和數(shù)組。
相對(duì)而言,個(gè)人還是比較的偏向對(duì)象的寫法,可能 react 寫習(xí)慣了吧。原因有兩點(diǎn):
- 數(shù)組能實(shí)現(xiàn)的,對(duì)象也能使用(反之亦然)。
- 在 DOM 結(jié)構(gòu)中,使用數(shù)組的
[]形式,感覺看起來比較的怪異,復(fù)雜。
對(duì)象
我把對(duì)象的形式分為三種場景,分別如下:
場景一:循環(huán)列表,根據(jù)列表項(xiàng)的某屬性的不同而展示不同
<script setup lang="ts">
const list = [
{
id: "1",
name: "name1",
isActive: true,
},
{
id: "2",
name: "name2",
isActive: false,
},
{
id: "3",
name: "name3",
isActive: false,
},
];
</script>
<template>
<div
v-for="item in list"
:key="item.id"
class="common"
:class="{ isActive: item.isActive }"
>
{{ item.name }}
</div>
</template>
<style>
.isActive {
color: red;
}
</style>
根據(jù)列表項(xiàng)的isActive的屬性值不同,來判斷是否顯示isActive類名。
場景二:通過觸發(fā)事件,來展示不同的樣式。
這種情況一般針對(duì)于用戶操作,比如點(diǎn)擊按鈕觸發(fā)事件,來修改某一內(nèi)容的樣式。
<script setup lang="ts">
import { ref } from "vue";
const isActive = ref<boolean>(false);
// 事件改變 data 屬性值
const btn = () => {
isActive.value = !isActive.value;
};
</script>
<template>
<div class="common" :class="{ isActive: isActive }">文字說明</div>
<button @click="btn">改變樣式</button>
</template>
<style>
.common {
font-size: 20px;
}
.isActive {
color: red;
}
通過點(diǎn)擊事件,修改 data 中的值,從而影響到 div 標(biāo)簽的類名展示。
情況三:當(dāng)關(guān)聯(lián)多個(gè)動(dòng)態(tài)樣式,直接綁定一個(gè)對(duì)象
上面的情況,當(dāng)存在少量的動(dòng)態(tài)樣式(1~2個(gè))的時(shí)候,可以直接寫在DOM結(jié)構(gòu)中,閱讀性可觀。當(dāng)存在多個(gè)的時(shí)候,還這樣寫的話,可能 DOM 就顯的混亂了,閱讀性極低,這時(shí)借助一個(gè)對(duì)象就很好的解決了這個(gè)問題。
<script setup lang="ts">
import { reactive } from "vue";
const classObj = reactive({
isActive: true,
hasError: false,
textRed: true,
});
</script>
<template>
<div class="common" :class="classObj">文字說明</div>
</template>
數(shù)組
數(shù)組的寫法,對(duì)于個(gè)人來說,就大致的了解下就行了。
形式一:綁定多個(gè)class
<script setup lang="ts">
import { ref } from "vue";
const activeClass = ref("active");
const errorClass = ref("text-danger");
</script>
<template>
<div :class="[activeClass, errorClass]"></div>
</template>
最后渲染出來的結(jié)果:
<div class="active text-danger"></div>
形式二:數(shù)組中的判斷
<!--三目運(yùn)算符的判斷-->
<div :class="[isActive ? activeClass : '', errorClass]"></div>
<!--數(shù)組中嵌套對(duì)象的判斷-->
<div :class="[{activeClass: isActive}, errorClass]"></div>
vue 中的動(dòng)態(tài) style 跟 class 的用法基本相似的,就不用多說了。
透傳的attributes
在上面的樣式綁定中,都是把 class 寫在了原生標(biāo)簽上,那么如果把 class 寫在組件上,效果是什么呢?
在此之前,先來彌補(bǔ)兩個(gè)小知識(shí)點(diǎn)。
知識(shí)點(diǎn)一:vue3 支持多個(gè)根節(jié)點(diǎn)
<!--vue2: 錯(cuò)誤的寫法--> <template> <div></div> <div></div> </template> <!--vue3: 正確--> <template> <div></div> <div></div> </template>
知識(shí)點(diǎn)二:什么是透傳的attributes?
“透傳 attribute”指的是傳遞給一個(gè)組件,卻沒有被該組件聲明為 props 或 emits 的 attribute 或者 v-on 事件監(jiān)聽器。常見的有 class style id.
簡單的理解就是:傳遞給子組件的屬性,但是被沒有在子組件聲明,就是透傳 attributes。
準(zhǔn)備工作完成,可以開始正題了。
透傳 attributes 之樣式綁定
分為兩種情況,只有一個(gè)根節(jié)點(diǎn)或者多個(gè)根節(jié)點(diǎn)。
情況一:子組件只有一個(gè)根節(jié)點(diǎn)
<!--son子組件--> <template> <div class="own">son組件</div> </template>
存在自身的類名 own
<!--父組件使用Son--> <Son class='abc' />
class 并沒有在 props 中申明,那么它就是透傳 attributes。
那么最后渲染的結(jié)果:
<div class='abc own'>son組件</div>
就會(huì)主動(dòng)的綁定到根節(jié)點(diǎn)上去,與原來的 class 進(jìn)行組合。
情況二:子組件有多個(gè)根節(jié)點(diǎn)
存在多個(gè)根節(jié)點(diǎn)的時(shí)候,并且還傳遞了透傳 attributes,如果沒有手動(dòng)處理的話,是會(huì)存在警告的。
<!--son子組件--> <template> <div class="own">son組件1</div> <div class="own">son組件2</div> </template>
<!--父組件使用Son--> <Son class='abc' />
沒有處理,拋出警告:

那么處理警告的方式兩種:
- 禁用透傳 attributes,設(shè)置
inheritAttrs為 false(后面再說)。 - 手動(dòng)處理,具體綁定在哪一個(gè)根節(jié)點(diǎn)。
<!--son子組件--> <template> <div class="own" :class="$attrs['class']">son組件1</div> <div class="own">son組件2</div> </template>
這樣警告也會(huì)消失,并且把透傳 attributes 綁定在了第一個(gè)根節(jié)點(diǎn)上。這里的$attrs是一個(gè)對(duì)象,需要具體指明某個(gè)屬性。
最后渲染的結(jié)果:
<div class="own abc">son組件1</div> <div class="own">son組件2</div>
透傳 attributes 之事件綁定
上面只是針對(duì)樣式進(jìn)行了透傳,那么事件的話,又會(huì)是怎么樣的呢?
先說結(jié)論吧,表現(xiàn)形式跟樣式綁定是基本一樣的。
<!--子組件Son-->
<script setup lang="ts">
const btn1 = () => {
console.log("子組件的點(diǎn)擊事件");
};
</script>
<template>
<button @click="btn1">點(diǎn)擊</button>
</template>
現(xiàn)在子組件的根節(jié)點(diǎn)是一個(gè) button 標(biāo)簽,并且上面綁定了一個(gè)點(diǎn)擊事件。
<!--父組件使用-->
<script setup lang="ts">
import Son from "./Son.vue";
const btn = () => {
console.log("父組件的點(diǎn)擊事件");
};
</script>
<template>
<Son @click="btn" />
</template>
父組件調(diào)用,也傳遞了一個(gè)透傳的事件過來。當(dāng)點(diǎn)擊按鈕:

發(fā)現(xiàn),子組件的事件被觸發(fā)了,父組件傳遞過來的事件也被觸發(fā)了,順序?yàn)?strong>先子后父。
透傳的 attributes 基本說完了,接下來就看看幾個(gè)特殊部分吧。
特殊1:組件嵌套
有些情況下一個(gè)組件會(huì)在根節(jié)點(diǎn)上渲染另一個(gè)組件。那么透傳 attributes 也會(huì)繼續(xù)傳遞下去。
<!--baseChild--> <template> <div></div> </template> <!--child--> <template> <base-child /> </template> <!--father--> <Child class='abc'/>
那么最后渲染的結(jié)果:
<div class='abc'></div>
特殊2:禁用透傳attributes
設(shè)置 inheritAttrs:false,就禁止使用了自動(dòng)綁定,可以消除前面所說的警告;然后就可以自由的綁定 $attrs, 該對(duì)象就包含了傳遞過來的透傳 attributes。
<!--如果是setup寫法,就需要單獨(dú)添加一個(gè)script標(biāo)簽,暴露一個(gè)配置對(duì)象-->
<script lang="ts">
export default {
inheritAttrs: false, // 禁用
};
</script>
<script setup lang="ts">
const btn1 = () => {
console.log("子組件的點(diǎn)擊事件");
};
</script>
<template>
<button @click="btn1">點(diǎn)擊</button>
</template>
需要注意的是:
- 和 props 有所不同,透傳 attributes 在 JavaScript 中保留了它們?cè)嫉拇笮?/strong>,所以像 foo-bar 這樣的一個(gè) attribute 需要通過
$attrs['foo-bar']來訪問。 - 像 @click 這樣的一個(gè) v-on 事件監(jiān)聽器將在此對(duì)象下被暴露為一個(gè)函數(shù)
$attrs.onClick。
特殊3:在 javascript 中訪問透傳的attributes
在組件實(shí)例中獲取,通過 this 的形式
export default {
created() {
console.log(this.$attrs)
}
}
總結(jié)
回顧了vue中動(dòng)態(tài)綁定樣式,還是比較簡單;又新學(xué)習(xí)了一點(diǎn) vue3 的新知識(shí):透傳attributes,收獲滿滿。
參考文檔
以上就是vue3中的透傳attributes教程示例詳解的詳細(xì)內(nèi)容,更多關(guān)于vue3透傳attributes的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue和relation-graph庫打造高質(zhì)量的關(guān)系圖應(yīng)用程序
這篇文章主要介紹了Vue和relation-graph庫打造高質(zhì)量的關(guān)系圖應(yīng)用程序,在這篇文章中,我們深入探討了如何使用Vue和relation-graph高效打造關(guān)系圖,我們?cè)敿?xì)介紹了數(shù)據(jù)準(zhǔn)備、關(guān)系映射、點(diǎn)擊事件等關(guān)鍵步驟,需要的朋友可以參考下2023-09-09
關(guān)于Vue的?Vuex的4個(gè)輔助函數(shù)
這篇文章主要介紹了關(guān)于Vue的?Vuex的4個(gè)輔助函數(shù),輔助函數(shù)的好處就是幫助我們簡化了獲取store中state、getter、mutation和action,下面我們一起來看看文章具體的舉例說明吧,需要的小伙伴也可以參考一下2021-12-12
vue實(shí)現(xiàn)數(shù)字動(dòng)態(tài)翻牌的效果(開箱即用)
這篇文章主要介紹了vue實(shí)現(xiàn)數(shù)字動(dòng)態(tài)翻牌的效果(開箱即用),實(shí)現(xiàn)原理是激將1到9的數(shù)字豎直排版,通過translate移動(dòng)位置顯示不同數(shù)字,本文通過實(shí)例代碼講解,需要的朋友可以參考下2019-12-12
詳解vue結(jié)合el-table實(shí)現(xiàn)表格小計(jì)總計(jì)需求(summary-method)
這篇文章主要介紹了vue結(jié)合el-table實(shí)現(xiàn)表格小計(jì)總計(jì)需求(summary-method),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
uniapp開發(fā)打包成H5部署到服務(wù)器的詳細(xì)步驟
前端使用uniapp開發(fā)項(xiàng)目完成后,需要將頁面打包,生成H5的靜態(tài)文件,部署在服務(wù)器上這樣通過服務(wù)器鏈接地址,直接可以在手機(jī)上點(diǎn)開來訪問,下面小編給大家講解uniapp開發(fā)打包成H5部署到服務(wù)器的步驟,感興趣的朋友一起看看吧2022-11-11

