Created
November 30, 2022 07:30
-
-
Save frama21/998dc1735964e83a6feea2050375b1d6 to your computer and use it in GitHub Desktop.
Render Component Via HTML in Vue JS
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
<script> | |
// vue | |
import Vue from 'vue/dist/vue.esm'; | |
// lodash | |
import unescape from 'lodash/unescape'; | |
// component | |
import AppBaseParameterEditor from './AppBaseParameterEditor'; | |
export default { | |
components: { AppBaseParameterEditor }, | |
props: { | |
dataHtml: { | |
type: String, | |
default: '' | |
} | |
}, | |
data() { | |
return { templateRender: null }; | |
}, | |
computed: { | |
htmlAppend() { | |
return `<div id="preview-editor">${this.dataHtml}</div>`; | |
} | |
}, | |
watch: { | |
dataHtml: { | |
handler(val) { | |
if (val) { | |
this.updateRender(); | |
} | |
}, | |
immediate: true | |
} | |
}, | |
methods: { | |
/** | |
* @description for update render data & compile the data | |
*/ | |
updateRender() { | |
const res = Vue.compile(unescape(this.htmlAppend)); | |
this.templateRender = res.render; | |
// staticRenderFns belong into $options, | |
// appearantly | |
this.$options.staticRenderFns = []; | |
// clean the cache of static elements | |
// this is a cache with the results from the staticRenderFns | |
this._staticTrees = []; | |
// Fill it with the new staticRenderFns | |
for (const i in res.staticRenderFns) { | |
//staticRenderFns.push(res.staticRenderFns[i]); | |
this.$options.staticRenderFns.push(res.staticRenderFns[i]); | |
} | |
} | |
}, | |
/** | |
* @description for render & create element | |
*/ | |
render(createElement) { | |
if (!this.templateRender) { | |
return createElement('div', 'loading...'); | |
} else { | |
return this.templateRender(); | |
} | |
} | |
}; | |
</script> | |
<style scoped> | |
p { | |
margin: 0; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment