vue中Props與Attrs的實(shí)現(xiàn)實(shí)例
基本功能
- props常用來數(shù)據(jù)通信
- attars所有非props屬性的透穿
props傳值:
基本傳值
<!-- 父組件 -->
<ChildComponent title="歡迎頁面" />
<!-- 子組件 -->
<script>
export default {
props: ['title']
}
</script>
動(dòng)態(tài)綁定
<!-- 父組件 -->
<ChildComponent :count="dynamicValue" />
<!-- 子組件 -->
<script>
export default {
props: ['count']
}
</script>
類型驗(yàn)證
// 子組件
props: {
age: {
type: Number,
required: true,
validator: value => value > 0
}
}
默認(rèn)值設(shè)置
props: {
theme: {
type: String,
default: 'light'
}
}
對(duì)象形式傳值
<!-- 父組件 --> <UserProfile v-bind="userData" /> <!-- 等價(jià)于 --> <UserProfile :name="userData.name" :age="userData.age" :email="userData.email" />
attars屬性透穿
屬性透穿的主要特點(diǎn)既然是接受非props以外的屬性,前端可以主動(dòng)接受某些特定的屬性,也可以不接受這些屬性。
實(shí)際應(yīng)用場(chǎng)景如下:
組件的二次封裝,比如常見的按鈕:
子組件
<template>
<!-- 按鈕組件,使用動(dòng)態(tài)class綁定 -->
<button
class="base-btn"
:class="computedClasses"
v-bind="attrs" <!-- 透?jìng)髌渌麑傩裕ㄈ鏳isabled、title等) -->
>
<slot></slot>
</button>
</template>
<script setup>
import { useAttrs, computed } from 'vue'
// 獲取所有透?jìng)鞯姆莗rops屬性
const attrs = useAttrs()
// 根據(jù)透?jìng)鞯膶傩杂?jì)算樣式類
const computedClasses = computed(() => {
const classes = []
// 如果透?jìng)髁藅ype屬性,添加對(duì)應(yīng)的樣式類
if (attrs.type) {
classes.push(`btn-${attrs.type}`)
}
// 如果透?jìng)髁藄ize屬性,添加對(duì)應(yīng)的樣式類
if (attrs.size) {
classes.push(`btn-${attrs.size}`)
}
// 如果透?jìng)髁藃ound屬性,添加圓角樣式
if (attrs.round) {
classes.push('btn-round')
}
// 如果透?jìng)髁藄pecial屬性,添加特殊樣式
if (attrs.special) {
classes.push('btn-special')
}
return classes
})
</script>
<style scoped>
/* 基礎(chǔ)按鈕樣式 */
.base-btn {
padding: 8px 16px;
border: none;
cursor: pointer;
font-size: 14px;
transition: all 0.3s ease;
}
/* 不同類型按鈕的樣式 */
.btn-primary {
background-color: #42b983;
color: white;
}
.btn-danger {
background-color: #ff4444;
color: white;
}
.btn-warning {
background-color: #ffbb33;
color: black;
}
/* 不同尺寸按鈕的樣式 */
.btn-small {
padding: 4px 8px;
font-size: 12px;
}
.btn-large {
padding: 12px 24px;
font-size: 16px;
}
/* 圓角樣式 */
.btn-round {
border-radius: 20px;
}
/* 特殊樣式 */
.btn-special {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-transform: uppercase;
letter-spacing: 1px;
}
/* 鼠標(biāo)懸停效果 */
.base-btn:hover {
opacity: 0.9;
transform: translateY(-2px);
}
</style>
父組件
<template>
<div class="button-group">
<!-- 基礎(chǔ)按鈕 -->
<CustomButton>基礎(chǔ)按鈕</CustomButton>
<!-- 主要按鈕(帶類型) -->
<CustomButton type="primary">主要按鈕</CustomButton>
<!-- 危險(xiǎn)按鈕(帶類型和尺寸) -->
<CustomButton type="danger" size="large">危險(xiǎn)按鈕</CustomButton>
<!-- 警告按鈕(帶類型、尺寸和圓角) -->
<CustomButton type="warning" size="small" round>警告按鈕</CustomButton>
<!-- 特殊按鈕(帶自定義屬性) -->
<CustomButton special title="這是一個(gè)特殊按鈕">特殊按鈕</CustomButton>
<!-- 禁用狀態(tài)按鈕(透?jìng)髟鷮傩裕?-->
<CustomButton type="primary" disabled>禁用按鈕</CustomButton>
</div>
</template>
<script setup>
import CustomButton from './CustomButton.vue'
</script>
<style scoped>
.button-group {
display: flex;
gap: 10px;
padding: 20px;
flex-wrap: wrap;
}
</style>
到此這篇關(guān)于vue中Props與Attrs的實(shí)現(xiàn)實(shí)例的文章就介紹到這了,更多相關(guān)vue Props Attrs內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)移動(dòng)端可拖拽式icon圖標(biāo)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動(dòng)端可拖拽式icon圖標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue2中provide/inject的使用與響應(yīng)式傳值詳解
Vue中 Provide/Inject實(shí)現(xiàn)了跨組件的通信,下面這篇文章主要給大家介紹了關(guān)于vue2中provide/inject的使用與響應(yīng)式傳值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Vue.extend實(shí)現(xiàn)組件庫(kù)message組件示例詳解
這篇文章主要為大家介紹了Vue.extend實(shí)現(xiàn)組件庫(kù)message組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
基于vue3實(shí)現(xiàn)一個(gè)抽獎(jiǎng)小項(xiàng)目
在公司年會(huì)期間我做了個(gè)抽獎(jiǎng)小項(xiàng)目,非常棒,今天把他分享到腳本之家平臺(tái),供大家學(xué)習(xí)參考,對(duì)vue3實(shí)現(xiàn)抽獎(jiǎng)小項(xiàng)目感興趣的朋友一起看看吧2023-01-01

