vue 動態創建組件(運行時創建組件)


 

  function mountCmp (cmp, props, parent) {
    if (cmp.default) {
      cmp = cmp.default
    }
    cmp = Vue.extend(cmp)
    let node = document.createElement('div')
    parent.appendChild(node)
    new cmp({ // eslint-disable-line
      el: node,
      propsData: props,
      parent: this
    })
  }

 

 


      import('../components/title').then(cmp => {
        mountCmp.call(this, cmp, {title: 123456}, document.querySelector('.child-host'))
        mountCmp.call(this, cmp, {title: 123456}, document.querySelector('.child-host'))
      })

 

 

   

 

//title.vue

<template>
    <div class="title">
      <div class="title-icon"></div>
      <div class="title-txt">{{title}}</div>
      <div class="title-dotline">&nbsp;</div>
    </div>
</template>


<script>
export default {
  props: ['title']
}
</script>

 


function mountCmp ( cmp, props, parent) {
cmp = Vue. extend( cmp. default)
let node = document. createElement( 'div')
parent. appendChild( node)
new cmp({ // eslint-disable-line
el: node,
propsData: props,
parent: this
})
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM