How to add dynamic components/partials in Vue.js

后端 未结 2 1151
时光说笑
时光说笑 2021-02-06 04:17

I need to add child components dynamically to a component based on user interaction. I looked to some old issue but it seems like a dirty hack to me, besides, it is an old issue

2条回答
  •  一生所求
    2021-02-06 04:53

    You want to put the custom components there right from the start, and use v-if or v-for to show, hide, add, or subtract these components. You let the data drive the DOM, not managing the DOM yourself. Fiddle example: https://jsfiddle.net/f5n5z5oo/2/

    You can even make the components dynamically change what type of component they are using is:

    data: {
      elements: [
        { type: 'component-a' },
        { type: 'component-b' }
      ]
    }
    

    If you're more specific about your use case I will try to help further!

提交回复
热议问题