Created
September 7, 2020 09:12
-
-
Save tangoslee/e102848a9f970c6b1a571b085363277e to your computer and use it in GitHub Desktop.
A way to return Vue component programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// import { uuid } from 'vue-uuid'