I have a component called CanvasComp which I am importing from my components folder. I am rendering this component on my template like <CanvasComp :jsoData="data"/>. It is sending the jsonData to the component. What I want is to render this component dynamically without having to display it manually on the template then pass the jsonData dynamically also.Below is my current implementation:
<template>
<ion-page id="main-content">
<CanvasComp :jsoData="data"/>
</ion-page>
</template>
<script lang="ts">
import { defineComponent, onBeforeMount, onMounted, ref, watch, watchEffect, reactive, computed } from "vue";
import { IonContent,IonPage } from "@ionic/vue";
import CanvasComp from "@/components/CanvasComp.vue";
export default defineComponent({
name: "GraphView",
components: {
IonContent,
IonPage,
CanvasComp,
},
setup() {
const data= [
{ id: 1, name: 'Default' },
{ id: 2, name: 'Demographics'},
{ id: 3, name: 'Other'},
];
}
return { data, CanvasComp }
</script>
Is there a way I can render that <CanvasComp :jsoData="data"/> in my template dynamically while passing the jsonData as well?
e.g const CanvasComp = prop{jsonData: data}; then return it.
Not sure if this is what you need, however there is a way how to dynamically render a component into the template:
Lets say we have a simple component
components/HelloWorld.vuewhich accepts atextprop and renders it in an<h1>element:Now I have another component
components/Wrapper.vuewhich uses the<component>element to render a dynamic component based on the value of thedynamicComponentdata property.In the example above the
dynamicComponentdata property is a function that returns the importedHelloWorldcomponent, and thedynamicTextdata property is a string with the value"Hello people!". This value will be passed as thetextprop to theHelloWorldcomponent when it's rendered.Here is a link to a working DEMO.
In your case I imagine you could dynamically render the
CanvasCompcomponent the same way I have rendered myHelloWorldcomponent. And of course, instead of thetextprop you would use thejsoDataprop.