Vuejs nested slots: how to pass slot to grandchild

后端 未结 1 1674
感情败类
感情败类 2020-12-06 02:40

I use different vuetify components, for example v-menu. It has a template like this:


  menu
  

        
相关标签:
1条回答
  • 2020-12-06 03:05

    If you put the slot attribute on a html element, that html element is passed to the child component to fill the slot with that name. If you don't want to pass along a html element, you can use slot on a template tag within your component. A template tag groups elements, but does not render to a html element, which is perfect here. You can use template tags also for other things, such as to group elements in a v-if for example, or to repeat multiple elements with a v-for.

    // App.vue
    <template>
      <div id="app">
        <test>
          <template slot="activator">
            Click <b>me</b>!
          </template>
        </test>
      </div>
    </template>
    
    // Test.vue
    <template>
      <div class="wrapper">
        <grand-child>
          <template slot="activator">
            <slot name="activator"></slot>
          </template>
        </grand-child>
    
        This is some text
      </div>
    </template>
    
    // GrandChild.vue
    <template>
      <div>
        <a href="#" @click="toggle = !toggle">
          <slot name="activator">Default</slot>
        </a>
    
        <div v-if="toggle">This appears and disappears</div>
      </div>
    </template>
    

    Edit: If you want to do this for arbitrary slots, this is also possible. this.$slots contains the slots and their content, so with something like the following, you can pass the slot content to a slot with the same name:

    <grand-child>
      <template v-for="(_, slot) in $slots">
        <template :slot="slot">
          <slot :name="slot"></slot>
        </template>
      </template>
    </grand-child>
    

    For completeness sake, scoped slots can be accessed through $scopedSlots and be propagated like so:

    <grand-child>
      <template v-for="(_, slot) in $scopedSlots" v-slot:[slot]="props">
        <slot :name="slot" v-bind="props" />
      </template>
    </grand-child>
    

    source and comment

    0 讨论(0)
提交回复
热议问题