1、App.vue程式碼如下:
<template> <div> <h3>主頁面</h3> <Slot001> <template #s1="slotProps"> <h4>{{ msg }} === {{ slotProps.msg1 }}</h4> </template> <template #s2="slotProps"> <h4>{{ msg }} === {{ slotProps.msg2 }}</h4> </template> </Slot001> </div> </template> <script> import Slot001 from './view/Slot001.vue' export default { data() { return { msg: '我是主頁面內容' } }, components: { Slot001 } } </script>
2、Slot001.vue程式碼如下:
<template> <h3>插槽頁面</h3> <slot name="s1" :msg1="childMsg1">預設1</slot> <slot name="s2" :msg2="childMsg2">預設2</slot> </template> <script> export default { data() { return { childMsg1: '子元件傳遞過來的訊息1', childMsg2: '子元件傳遞過來的訊息2', } } } </script>
3、效果如下: