欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中Props與Attrs的實(shí)現(xiàn)實(shí)例

 更新時間:2025年09月11日 09:37:17   作者:拉不動的豬  
本文主要介紹了vue中Props與Attrs的實(shí)現(xiàn)實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

基本功能

  1. props常用來數(shù)據(jù)通信
  2. attars所有非props屬性的透穿

props傳值:

基本傳值‌

<!-- 父組件 -->
<ChildComponent title="歡迎頁面" />

<!-- 子組件 -->
<script>
export default {
  props: ['title']
}
</script>

動態(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'
  }
}

對象形式傳值‌

<!-- 父組件 -->
<UserProfile v-bind="userData" />

<!-- 等價(jià)于 -->
<UserProfile 
  :name="userData.name"
  :age="userData.age"
  :email="userData.email" 
/>

attars屬性透穿

屬性透穿的主要特點(diǎn)既然是接受非props以外的屬性,前端可以主動接受某些特定的屬性,也可以不接受這些屬性。

實(shí)際應(yīng)用場景如下:

組件的二次封裝,比如常見的按鈕:

子組件

<template>
  <!-- 按鈕組件,使用動態(tài)class綁定 -->
  <button 
    class="base-btn" 
    :class="computedClasses" 
    v-bind="attrs"  <!-- 透傳其他屬性(如disabled、title等) -->
  >
    <slot></slot>
  </button>
</template>

<script setup>
import { useAttrs, computed } from 'vue'

// 獲取所有透傳的非props屬性
const attrs = useAttrs()

// 根據(jù)透傳的屬性計(jì)算樣式類
const computedClasses = computed(() => {
  const classes = []
  
  // 如果透傳了type屬性,添加對應(yīng)的樣式類
  if (attrs.type) {
    classes.push(`btn-${attrs.type}`)
  }
  
  // 如果透傳了size屬性,添加對應(yīng)的樣式類
  if (attrs.size) {
    classes.push(`btn-${attrs.size}`)
  }
  
  // 如果透傳了round屬性,添加圓角樣式
  if (attrs.round) {
    classes.push('btn-round')
  }
  
  // 如果透傳了special屬性,添加特殊樣式
  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="這是一個特殊按鈕">特殊按鈕</CustomButton>
    
    <!-- 禁用狀態(tài)按鈕(透傳原生屬性) -->
    <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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • vue實(shí)現(xiàn)移動端可拖拽式icon圖標(biāo)

    vue實(shí)現(xiàn)移動端可拖拽式icon圖標(biāo)

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動端可拖拽式icon圖標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 關(guān)于VUE的編譯作用域及slot作用域插槽問題

    關(guān)于VUE的編譯作用域及slot作用域插槽問題

    這篇文章主要介紹了VUE 的編譯作用域及slot作用域插槽問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • vue 國際化 vue-i18n 雙語言 語言包

    vue 國際化 vue-i18n 雙語言 語言包

    這篇文章主要介紹了vue 國際化 vue-i18n 雙語言 語言包的相關(guān)知識,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • vue實(shí)現(xiàn)二維碼掃碼功能(帶樣式)

    vue實(shí)現(xiàn)二維碼掃碼功能(帶樣式)

    最近接了一個移動端的項(xiàng)目,實(shí)現(xiàn)微信掃碼功能,今天小編利用這個平臺給大家分享vue實(shí)現(xiàn)二維碼掃描功能的實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2021-08-08
  • 解決vue 單文件組件中樣式加載問題

    解決vue 單文件組件中樣式加載問題

    這篇文章主要介紹了解決vue 單文件組件中樣式加載問題,文章末尾給大家補(bǔ)充介紹了vue單文件組件中樣式的問題,需要的朋友可以參考下
    2019-04-04
  • vue2中provide/inject的使用與響應(yīng)式傳值詳解

    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頁面加載閃爍問題的解決方法

    vue頁面加載閃爍問題的解決方法

    這篇文章主要介紹了vue頁面加載閃爍問題的解決方法,文中給大家提到了v-if 和 v-show 的區(qū)別,解決vue頁面加載時出現(xiàn){{message}}閃退的兩種方法,感興趣的朋友一起看看吧
    2018-03-03
  • Vue.extend實(shí)現(xiàn)組件庫message組件示例詳解

    Vue.extend實(shí)現(xiàn)組件庫message組件示例詳解

    這篇文章主要為大家介紹了Vue.extend實(shí)現(xiàn)組件庫message組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Vue 中頁面?zhèn)髦档亩喾N方式小結(jié)

    Vue 中頁面?zhèn)髦档亩喾N方式小結(jié)

    本文主要介紹了Vue 中頁面?zhèn)髦档亩喾N方式小結(jié),主要包括路由傳參、Vuex 狀態(tài)管理、Props 屬性和事件傳遞數(shù)據(jù)這幾種,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • 基于vue3實(shí)現(xiàn)一個抽獎小項(xiàng)目

    基于vue3實(shí)現(xiàn)一個抽獎小項(xiàng)目

    在公司年會期間我做了個抽獎小項(xiàng)目,非常棒,今天把他分享到腳本之家平臺,供大家學(xué)習(xí)參考,對vue3實(shí)現(xiàn)抽獎小項(xiàng)目感興趣的朋友一起看看吧
    2023-01-01

最新評論