Humoyun Ahmad
Humoyun Ahmad

Reputation: 3081

Vue Dynamic Components: how to select from multiple instance of the same component

I have the following problem: I am using iView as UI lib in our project, and I have to choose Button out of several same iView Button components inside dynamic component, what to pass to :is props of component. Here is an excerpt from my code:

  <span class="top-buttons" v-if="showTopButtons">
    <Button @click="selectAll">
      <Icon type="android-remove-circle"></Icon>
      Select All
    </Button>
    <component :is="???">
      <Button @click="moveToDrafts">
        <Icon type="android-cancel"></Icon>
        Move to Drafts
      </Button>
      <Button @click="publish">
        <Icon type="android-cancel"></Icon>
        Publish
      </Button>
      <Button @click="publish">
        <Icon type="android-cancel"></Icon>
        Publish
      </Button>
    </component>
    <Button @click="deleteTour">
      <Icon type="trash-a"></Icon>
      Delete
    </Button>
  </span>

Upvotes: 1

Views: 1087

Answers (1)

Jacob Goh
Jacob Goh

Reputation: 20845

:is prop should be passed a component

example:

<template>
    <component v-bind:is="currentTabComponent"></component>
</template>

<script>
import currentTabComponent from './currentTabComponent';
export default {
  components: {
    currentTabComponent,
  },
};
</script>

In you case, it's probably more suitable to use v-if instead

<Button @click="moveToDrafts" v-if="someCondition1">
    <Icon type="android-cancel"></Icon>
    Move to Drafts
</Button>
<Button @click="publish" v-else-if="someCondition2">
    <Icon type="android-cancel"></Icon>
    Publish
</Button>
<Button @click="publish" v-else>
    <Icon type="android-cancel"></Icon>
    Publish
</Button>

Upvotes: 2

Related Questions