I am working on a vue.js project(version 3). I come to a situation where I want to use the rendered HTML view of a component to my current component's method.
I created the following SVG component in my Vue project.
CircleWithTextSvg.vue
<template>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" width="20" height="20">
<g id="UrTavla">
<circle cx="10" cy="10" r="10" stroke="gray" stroke-width="1" :fill="fill" />
<text x="50%" y="50%" text-anchor="middle" stroke="black" stroke-width="1px" dy=".3em">{{text}}</text>
</g>
</svg>
</template>
<script>
export default {
props: {
text: {
type: String,
default: "1"
},
fill: {
type: String,
default: "white"
}
}
}
</script>
This component basically renders a circle with text inside. If I use this component in my main component's template section like the following then it works fine(obviously)
MainComponent.vue
<template>
<circle-with-text-svg />
</template>
But I want to send this SVG component rendered output as an option to the third party.
Real Use Case:- Actually, I created this separate component to show as a marker on my leaflet map. The problem is now I want to use this SVG component inside my MainComponent's method so I can use it as an option to L.divIcon
When I try the following
export default {
methods: {
generateMap(element) {
// ...
let icon = L.divIcon({
html: <circle-with-text-svg
fill="'#D3D5FF'"
text="1" />,
iconSize: [10, 10]
});
// ...
}
}
}
Then it gives the errors
Support for the experimental syntax 'JSX isn't currently enabled
In the react, we can simply use the component inside the template of another component normally. but how can we achieve this in vue.js
By looking at the error, it seems like JSX experimental is not enabled.
Can someone address me how can I achieve this?
createAppinstead ofnew Vuemainly) but the principle is the same - create new app, mount it, retrieve rendered HTML. Regarding the overhead -createAppis very similar as rendering component in template - both app and component are instances of Vue.