iT邦幫忙

2025 iThome 鐵人賽

DAY 21
0
Vue.js

Vue.js 新手入門之路系列 第 21

Vue.js 新手入門之路 - fallthrough

  • 分享至 

  • xImage
  •  

今天要來學習傳透屬性 fallthrough
先瞭解什麼是 fallthrough ,就是父元件傳給子元件的非 props 屬性,會自動加到子元件的根元素上

基礎 fallthrough

子元件:

<template>
  <button>Click Me</button>
</template>

父元件:

<script setup>
import MyButton from './components/fallthrough.vue'
</script>

<template>
  <MyButton class="large"/>
</template>

選染結果成功傳透
https://ithelp.ithome.com.tw/upload/images/20250910/20178296HaINao7cuP.png

合併 fallthrough

子元件:

<template>
  <button class="son">Click Me</button>
</template>

父元件:

<script setup>
import MyButton from './components/fallthrough.vue'
</script>

<template>
  <MyButton class="father" style="color:red;" />
</template>

可以看到 class 屬性合併了
https://ithelp.ithome.com.tw/upload/images/20250910/20178296ZGMp2try2m.png

事件繼承

子元件:

<template>
  <button @click="console.log('子元件點擊')">Click Me</button>
</template>

父元件:

<script setup>
import MyButton from './components/fallthrough.vue'
</script>

<template>
  <MyButton @click="console.log('父元件監聽')" />
</template>

父子事件會同時觸發
https://ithelp.ithome.com.tw/upload/images/20250910/20178296IsfLA1BbPW.png

ref:
https://zh-hk.vuejs.org/guide/components/attrs.html


上一篇
Vue.js 新手入門之路 - emit 事件
下一篇
Vue.js 新手入門之路 - fallthrough(二)
系列文
Vue.js 新手入門之路25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言