Vue.js mount component after DOM tree mutation to add a vue component

前端 未结 2 760
鱼传尺愫
鱼传尺愫 2021-02-10 18:34

I have a use case (below) where I need to mount (if thats the correct term) a Vue.js component template that was inserted into the DOM via jQuery, I can setup a Mut

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-10 19:05

    One way to instantiate Vue components in runtime-generated HTML is:

    var ComponentClass = Vue.extend({
        template: '...',
    });
    var instance = new ComponentClass({
        propsData: { name: value },
    });
    instance.$mount('#uid'); // HTML contains <... id="uid">
    ...
    instance.$destroy(); // if HTML containing id="uid" is dropped
    

    More here (I am not affiliated with this site)
    https://css-tricks.com/creating-vue-js-component-instances-programmatically/

提交回复
热议问题