Skip to content

How to render component from a string? #11174

Discussion options

You must be logged in to vote

It's actually quite simple to achieve:

<script setup>
import { ref } from 'vue'

const customTemplate = ref('<div>Runtime Template</div>')

setInterval(() => {
  // Simulate a template received from the server
  const newTemplate = `<div>Runtime Template: ${Math.random()}</div>`
  customTemplate.value = newTemplate
}, 500)
</script>

<template>
  <component :is="{ template: customTemplate }"></component>
</template>

https://play.vuejs.org/#eNp9U01r4zAQ/StCLCSB1C7s7sU4gd2lhy7sB22POtRrjxNlLclIoyTF+L93JGE3LaU3aWbe03tP0sC/9X129MALXrrayh6ZA/T9VmipemORDcxCy0bWWqPYgkYXQgtdG+2Q1d6hUQ+g+q5CYJswulyUjTxu77xGqYBNzTIP1cUqgOmEW41gj1W3XK7YZssGoRnLc3YvlY9UjWxbsKCR4cRuoQZ5hCYpcQQHG2BJiobThY7HdzUU7NPwq8J9Z…

Replies: 6 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ivanjaros
Comment options

Comment options

You must be logged in to vote
1 reply
@ivanjaros
Comment options

Answer selected by ivanjaros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants