[Vue] Dynamic component :is

We have a RadioGroupcomponent, when the prop verticalis true, we want all the Radiowrap with div, otherwise wrap with span.

<template>
  <component
    v-for="option in options"
    :key="option.value"
    :is="vertical ? 'div' : 'span'"
    :class="{
      horizontal: !vertical
    }"
  >
    <BaseRadio
      :label="option.label"
      :value="option.value"
      :modelValue="modelValue"
      :name="name"
      @update:modelValue="$emit('update:modelValue', $event)"
    />
  </component>
</template>

Parent component:

      <h3>Are pets allowed?</h3>
      <div>
        <BaseRadioGroup
          v-model="event.pets"
          name="pets"
          :options="petOptions"
          vertical
        />
      </div>

 

posted @ 2024-12-04 15:14  Zhentiw  阅读(4)  评论(0编辑  收藏  举报