instantiate-component.js 995 字节
Newer Older
M
init  
miaodian 已提交
1 2
export default function instantiateComponent(Vue, Component, data, renderFn) {
  let renderData
3
  let childrenRenderFn
M
init  
miaodian 已提交
4 5
  const instance = new Vue({
    render(createElement) {
6 7 8 9
      let children = childrenRenderFn && childrenRenderFn(createElement)
      if (children && !Array.isArray(children)) {
        children = [children]
      }
A
AmyFoxFN 已提交
10

A
AmyFoxFN 已提交
11
      // {...renderData}: fix #128, caused by vue modified the parameter in the version of 2.5.14+, which related to vue issue #7294.
A
AmyFoxFN 已提交
12
      return createElement(Component, {...renderData}, children || [])
M
init  
miaodian 已提交
13 14 15 16 17 18 19 20 21 22 23
    },
    methods: {
      init() {
        document.body.appendChild(this.$el)
      },
      destroy() {
        this.$destroy()
        document.body.removeChild(this.$el)
      }
    }
  })
24
  instance.updateRenderData = function (data, render) {
M
init  
miaodian 已提交
25
    renderData = data
26
    childrenRenderFn = render
M
init  
miaodian 已提交
27
  }
28
  instance.updateRenderData(data, renderFn)
M
init  
miaodian 已提交
29 30 31 32 33
  instance.$mount()
  instance.init()
  const component = instance.$children[0]
  return component
}