๐Ÿ”ธnamed slot

web โŸฉ component โŸฉ slot โŸฉ named slot

ๆœ‰ๅ‘ฝๅ็š„ <slot> ็จฑ็‚บ named slot๏ผŒๅฏๆŽงๅˆถๅ…งๅฎน็š„ๆ’ๅ…ฅไฝ็ฝฎใ€‚ๅฆ‚๏ผš

<!-- 1๏ธโƒฃ ๅฎšๅ๏ผšๅœจ <template> ๅ…ง้ƒจๅฎš็พฉ -->
<slot name="header"></slot>

<!-- 2๏ธโƒฃ ๆŒ‡ๅ๏ผšๅœจๅค–้ƒจ HTML ไฝฟ็”จ -->
<my-component>
    <h1 slot="header">This is the header!</h1>
</my-component>

็”จๆณ•

๐Ÿ“ <template> ๅ…ง้ƒจ็ตๆง‹๏ผšๅฎš็พฉๅ…ทๅๆ’ๆงฝ (named slot) ่ˆ‡้ ่จญๆ’ๆงฝ

<template id="my-component-template">
  <div class="container">
    
    <!-- โญ๏ธ named slot: header -->
    <slot name="header"></slot>
    
    <div class="content">
    
      <!-- โญ๏ธ ๆฒ’ๆœ‰ๅๅญ—็š„ๅฐฑๆ˜ฏใ€Œ้ ่จญๆ’ๆงฝใ€(default slot) -->
      <slot></slot> 
      
      <p>This is more default text inside content.</p>
    </div>
    
    <!-- โญ๏ธ named slot: footer -->
    <slot name="footer"></slot>
    
  </div>
</template>

Last updated

Was this helpful?