首页 > 解决方案 > Vue动态组件:如何从同一组件的多个实例中进行选择

问题描述

我有以下问题:我在我们的项目中使用 iView 作为 UI 库,我必须从动态组件中的几个相同的 iView Button 组件中选择Button,传递给 :is 组件的内容是什么。这是我的代码的摘录:

  <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>

标签: javascriptvue.jscomponentsvue-componentiview

解决方案


:isprop 应该传递一个组件

例子:

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

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

在您的情况下,它可能更适合v-if使用

<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>

推荐阅读