It's often needed to patch and compile a HTML (in my case a SVG) returned from a backend.
I used only the simplest version as
import {compile} from 'vue';
const component = compile(text);
But now I patch the source with DOM and inject other components inside it.
For that I need to use app.component()
to register all the injected components.
That's bad obviously for several known reasons.
Could we inject locally imported components? Like:
import Tooltip from 'Tooltip.vue';
import {compile} from 'vue';
const component = compile(text, {components:[Tooltip]});
Definitely compile
has an extensive set of options:
But I can't find anything about injecting components.
There are several ideas but need prolonged tries and errors like:
- Compile as SFC and inject dependencies somehow
- Provide components in
ctx
somehow on a render - Somehow do it with
defineComponent
But I can't figure a proper direction to follow, any help would be appreciated.
The only working way I've found so far is with <component>
but I'd like to use component names directly as tags (of cause I can patch the source to change custom components to <component>
but anyway):
<script setup>
import { compile, h } from 'vue'
const comp = compile('<component :is="test"></component>');
const test = () => h('div', 'TEST');
</script>
<template>
<comp :test/>
</template>