Skip to content

Instantly share code, notes, and snippets.

@tangoslee
Created September 7, 2020 09:12
Show Gist options
  • Save tangoslee/e102848a9f970c6b1a571b085363277e to your computer and use it in GitHub Desktop.
Save tangoslee/e102848a9f970c6b1a571b085363277e to your computer and use it in GitHub Desktop.
A way to return Vue component programmatically
const vueComponent = (component, propsData, uid = uuid.v1()) => {
// https://css-tricks.com/creating-vue-js-component-instances-programmatically/
const ComponentClass = Vue.extend(component)
const instance = new ComponentClass({ propsData })
instance.$mount()
const id = `tmp-${uid}`
window.setTimeout(() => {
// Replace dom
const old = document.querySelector(`#${id}`)
old.parentNode.replaceChild(instance.$el, old)
})
return `<div id="${id}"></div>`
}
@tangoslee
Copy link
Author

tangoslee commented Sep 7, 2020

  1. Return dummy div
  2. Create Vue Component
  3. Replace the dummy div to Vue Component

// import { uuid } from 'vue-uuid'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment