Could you provide an example to create a render function in runtime? #15
-
Thanks for the module. It saves me a lot! Currently, I am using the following code to create render function but not sure if it is the proper way. // DynamicComponent.vue
<script setup lang="ts">
import type { Component } from '@vue/runtime-core'
import { NuxtLink, SmartImage } from '#components'
const props = defineProps<{
content?: string
}>()
const renderer = computed(() => () => h({
template: props.content,
components: {
NuxtLink
},
}))
</script>
<template>
<component :is="renderer" />
</template> There is many example after searching, but quite confusing which one should I use. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 1 reply
-
Hi @shtse8 , there's many ways to create a component, i've merged a commit with the playground filled with components using the template prop. You can even generate a list of components from an API and use |
Beta Was this translation helpful? Give feedback.
-
@shtse8 i couldn't reproduce your issue, do your have a repo where i can reproduce it ? The I think that you can directly return a VNode in your computed const renderer = computed(() => h({
template: props.content,
components: {
NuxtLink
},
})) let me know if it has been resolved |
Beta Was this translation helpful? Give feedback.
-
My version is working, but not sure if it is the correct way to make it works. I am confusing to use |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
do you mean it is better to use |
Beta Was this translation helpful? Give feedback.
-
no i mean it's better to use h({
template: "<div></div>" <-- this
}) ssrCompile will compile the template then cache it. using import { compile } from "vue"
{
render: compile( "<div></div>" )
} make the server-renderer compile your template each time it need to render the component.
To pass your components, import them then pass it into the component just like you did here 👍 |
Beta Was this translation helpful? Give feedback.
-
Got it !! Huge thanks! |
Beta Was this translation helpful? Give feedback.
-
@huang-julien One more question, I am embedding twitter plugin to my article which is generated using runtime compiler. <blockquote class="twitter-tweet"><p lang="en" dir="ltr">So here’s what Tesla’s Bitcoin payment screen looks like <a href="https://t.co/vbUdidzURR">pic.twitter.com/vbUdidzURR</a></p>— Whole Mars Catalog (@WholeMarsBlog) <a href="https://twitter.com/WholeMarsBlog/status/1374600604337672193?ref_src=twsrc^tfw">March 24, 2021</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> Initial loading of page is working fine and the script will execute. But reenter the page, after routing in vue router, the script won't be executed. |
Beta Was this translation helpful? Give feedback.
-
@shtse8 i wouldn't recommend you to pass directly <blockquote class="twitter-tweet"><p lang="en" dir="ltr">So here’s what Tesla’s Bitcoin payment screen looks like <a href="https://t.co/vbUdidzURR">pic.twitter.com/vbUdidzURR</a></p>— Whole Mars Catalog (@WholeMarsBlog) <a href="https://twitter.com/WholeMarsBlog/status/1374600604337672193?ref_src=twsrc^tfw">March 24, 2021</a></blockquote>
<component is="script" async src="https://platform.twitter.com/widgets.js" charset="utf-8"></component>
|
Beta Was this translation helpful? Give feedback.
no i mean it's better to use
ssrCompile will compile the template then cache it.
using
compile
from vuemake the server-renderer compile your template each time it need to render the component.