Skip to content

Instantly share code, notes, and snippets.

@adrianjost
Created November 25, 2019 17:18
Show Gist options
  • Save adrianjost/3790b9d79321b8a9f66ae3900d2d1029 to your computer and use it in GitHub Desktop.
Save adrianjost/3790b9d79321b8a9f66ae3900d2d1029 to your computer and use it in GitHub Desktop.
RenderHtml Component
<script>
// Cool way to render Vue components from HTML Strings
// https://medium.com/haiiro-io/compile-markdown-as-vue-template-on-nuxt-js-1c606c15731c
import VueWithCompiler from "vue/dist/vue.esm";
export default {
props: {
html: {
type: String,
default: "",
},
},
data() {
return { templateRender: undefined };
},
watch: {
html(to) {
this.updateRender();
},
},
created() {
this.updateRender();
},
methods: {
updateRender() {
const compiled = VueWithCompiler.compile(this.html);
this.templateRender = compiled.render;
this.$options.staticRenderFns = [];
for (const staticRenderFunction of compiled.staticRenderFns) {
this.$options.staticRenderFns.push(staticRenderFunction);
}
},
},
render() {
return this.templateRender();
},
};
</script>
@ponceleon
Copy link

image

Hello friend, you will have any idea why I get this error.

I am running the code you shared on medium.

@adrianjost
Copy link
Author

adrianjost commented Jun 13, 2021

@ponceleon I guess it's because you are using Vue 3 (guessed because of vue@next) wich has a lot of API changes. I imagine the method I used doesn't work with Vue 3 anymore.

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