今天要來學習傳透屬性 fallthrough
先瞭解什麼是 fallthrough
,就是父元件傳給子元件的非 props
屬性,會自動加到子元件的根元素上
子元件:
<template>
<button>Click Me</button>
</template>
父元件:
<script setup>
import MyButton from './components/fallthrough.vue'
</script>
<template>
<MyButton class="large"/>
</template>
選染結果成功傳透
子元件:
<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
屬性合併了
子元件:
<template>
<button @click="console.log('子元件點擊')">Click Me</button>
</template>
父元件:
<script setup>
import MyButton from './components/fallthrough.vue'
</script>
<template>
<MyButton @click="console.log('父元件監聽')" />
</template>
父子事件會同時觸發
ref:
https://zh-hk.vuejs.org/guide/components/attrs.html