【问题标题】:wrap v-slot element in different parent based on condition根据条件将 v-slot 元素包装在不同的父级中
【发布时间】:2021-10-27 13:27:26
【问题描述】:

我有一个模板:

<template v-slot:title>
  {{ $t('title') }}
</template>

还有一个组件:

<v-col cols="12">
 <h5 class="text-h5">
   <slot name="title" />
 </h5>
</v-col>

我只想为一个特定实例使用&lt;h4&gt; 标记包装插槽。我尝试将其包装在模板中,但没有效果。如何交换包裹插槽的元素?

【问题讨论】:

    标签: vue.js vuejs2 v-slot


    【解决方案1】:

    您可以使用非强制 prop 将您想要的标签类型传递给组件,并根据传递的值将内容包装到动态组件中(在示例中 h2、h3 和 h4 是正确的值)

    在组件模板中:

    <v-col cols="12">
      <component :is="wrapper" :class="`text-${wrapper}`">
        <slot name="title" />
      </component>
    </v-col>
    

    在组件脚本中:

    props: {
      wrapper: {
        type     : String,
        required : false,
        default  : 'h5',
        validator: value => [ 'h2', 'h3', 'h4' ].includes(value)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 2021-04-10
      • 2019-03-06
      • 2014-09-20
      • 2016-05-04
      • 2023-01-25
      • 2019-01-05
      • 2020-07-29
      • 2023-03-19
      相关资源
      最近更新 更多