我有三个模板.AddCard.vue,图像卡.vue和视频卡.vue

AddCard.vue上有两个按钮,一个是添加图像卡,另一个是添加视频卡..我需要添加基于按钮点击的组件.这里有三个模板和我的索引.html文件.

指数html

   <!doctype html>
    <html lang="en">
        <head>

        </head>
        <body>
            <div id="app">
                 <div class="container">
                    <div id="dynamic_components">
                     <!-- add components here -->
                    </div>
                    <addcard></addcard>
                </div>
            </div>
            <script src="{{ asset('js/app.js') }}"></script>
        </body>
    </html>

AddCard.vue

<template>
    <div class="buttons">
        ul class="no-margin-list">
                                <li @click="imagecard">
                                  <span class="card_icon">
                                      <img :src="'img/image.jpg'" >
                                    </span>
                                    <p>Add Image Card</p>
                                  </a>
                                </li>

                            <li @click="videocard">
                                  <span class="card_icon">
                                      <img :src="'img/video.jpg'" >
                                    </span>
                                    <p>Add Video Card</p>
                                  </a>
                              </li>
    </div>
</template>
<script>
  export default {
    computed: {

    },
    methods: {
      imagecard(val) {
        //how to add image card
      },
      videocard() {
        //how to add video card 
      }
    },

  }
</script>

图像卡.vue

<template>
    <h1> I am a image Card </h1>
</template>
<script>

</script>

视频卡.vue

<template>
    <h1> I am a Video Card </h1>
</template>
<script>

</script>

我需要在<div id="dynamic_components">中一个接一个地动态添加组件.用户可以添加任意数量的卡片.

如何动态添加组件.请给我指一个教程.

推荐答案

使用v-for+dynamic component.

Vue.config.productionTip = false

Vue.component('card1', {
  template: '<div>Card:<span style="background-color:green">{{title}}</span></div>',
  props: ['title']
})

Vue.component('card2', {
  template: '<div>Card:<span style="background-color:blue">{{title}}</span></div>',
  props: ['title']
})

Vue.component('card3', {
  template: '<div>Card:<span style="background-color:yellow">{{title}}</span></div>',
  props: ['title']
})

new Vue({
  el: '#app',
  data() {
    return {
      cards: [
        {'card': {'title': 'I am one card1'}, 'card-type':'card1'},
        {'card': {'title': 'I am one card2'}, 'card-type':'card2'}
      ]
    }
  },
  computed: {
    computedNoCard1: function () {
      let availableCards = new Set(['card2', 'card3'])
      return this.cards.filter((item) => {
        return availableCards.has(item['card-type'])
      })
    }
  },
  methods: {
    addCard: function () {
      let supportedCards = ['card1', 'card2', 'card3']
      let seed = Math.floor(Math.random()*supportedCards.length)
      this.cards.push({'card': {'title': 'I am new card for ' + supportedCards[seed]}, 'card-type': supportedCards[seed]})
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<button @click="addCard()">Add Card</button>
  <table>
  <tr><th>Data Property</th><th>Computed Property</th></tr>
  <tr>
  <td>
    <div v-for="(card, index) in cards" :key="index">
      <component  :is="card['card-type']" :title="card.card.title">
      </component>
    </div>
  </td>
  <td>
    <div v-for="(card, index) in computedNoCard1" :key="index">
      <component  :is="card['card-type']" :title="card.card.title">
      </component>
    </div>
  </td>
  </tr>
  </table>
  
</div>

Vue.js相关问答推荐

正在try 访问vutify中v-select(ItemProps)的嵌套json值

在 VueJS 中,将变量而不是组件的字符串名称传递给动态组件的 :is 属性不起作用

使用计算(computed)属性更改视图

Vuetify/Nuxt 生产环境组件在开发环境中无法正常工作

如果找不到路由,Vue.js如何重定向到公共路由

如何在map中添加新的标记 onclick?

Vuetify v-data-table ,如何在 HTML 中呈现标题文本?

确保在渲染组件之前加载 Vuex 状态

Vuetify Datatable - 在所有列上启用内容编辑

更新 vue.js 中的数据属性/对象

错误 [ERR_UNSUPPORTED_ESM_URL_SCHEME]:默认 ESM 加载器仅支持文件和数据 URL - Vue 3

Vue Prop 没有初始化器,也没有在构造函数中明确赋值

用于本地编辑的 Vuex 克隆对象

Vuejs css 范围性能

过渡后的 v-if 下方的过渡元素

Vue cli 在 css 中构建生产更改不透明度

Vue $refs 对象的类型为 unknown未知

v-if 未在计算(computed)属性上触发

Vue中的嵌套循环

Vue.js 显示空格(换行符)