首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Vue在单击按钮时添加组件

Vue在单击按钮时添加组件
EN

Stack Overflow用户
提问于 2018-06-02 04:48:25
回答 1查看 26.7K关注 0票数 12

我有三个模板。AddCard.vue、ImageCard.vue和VideoCard.vue

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

index.html

代码语言:javascript
复制
   <!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

代码语言:javascript
复制
<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>

ImageCard.vue

代码语言:javascript
复制
<template>
    <h1> I am a image Card </h1>
</template>
<script>

</script>

VideoCard.vue

代码语言:javascript
复制
<template>
    <h1> I am a Video Card </h1>
</template>
<script>

</script>

我需要在<div id="dynamic_components">中一个接一个地动态添加组件。用户可以添加他们想要的任何数量的卡。

如何动态添加组件。请给我介绍一个教程。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-02 05:15:48

使用v-for + dynamic component

代码语言:javascript
复制
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]})
    }
  }
})
代码语言:javascript
复制
<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>

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50650815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档