vue 3, composition api, ts, sctipt setup. Render list of components using <components…
- User
vue 3, composition api, ts, sctipt setup. Render list of components using
- Assistant
Rendering a list of components dynamically using the
<component :is="">syntax in Vue 3 with the Composition API and TypeScript can be very effective for creating flexible and reusable UI elements. In this example, I'll guide you through the process.-
Set up your components: Define a couple of dummy components that you want to render dynamically.
-
Main component: Use the
<component>element to render the list of components dynamically.
Here's how you can do it step by step:
Step 1: Define individual components
First, let's create some example components to render dynamically.
// src/components/FirstComponent.vue <template> <p>This is the First Component</p> </template> <script lang="ts" setup> console.log("FirstComponent Loaded"); </script> <style scoped> /* Optional: Add styles here */ </style>// src/components/SecondComponent.vue <template> <p>This is the Second Component</p> </template> <script lang="ts" setup> console.log("SecondComponent Loaded"); </script> <style scoped> /* Optional: Add styles here */ </style>Step 2: Main Component to render the list
Now, let's create the main component that will render these components dynamically.
// src/components/DynamicList.vue <template> <div> <component v-for="(component, index) in components" :is="component" :key="index" /> </div> </template> <script lang="ts" setup> import { defineProps, ref } from 'vue'; import FirstComponent from './FirstComponent.vue'; import SecondComponent from './SecondComponent.vue'; // Array of components to be rendered const components = ref([FirstComponent, SecondComponent]); // If you prefer using defineProps for receiving data from a parent component // const props = defineProps<{ components: Array<typeof FirstComponent | typeof SecondComponent> }>(); // const components = ref(props.components); </script>Step 3: Use the Main Component
Finally, use the main component in your application.
// src/App.vue <template> <div id="app"> <DynamicList /> </div> </template> <script lang="ts" setup> import DynamicList from './components/DynamicList.vue'; </script> <style scoped> /* Optional: Add styles here */ </style>Explanation
-
Defining Child Components:
FirstComponentandSecondComponentare simple Vue components that display some text. -
Dynamic Component Rendering: In
DynamicList.vue, thecomponentsarray holds the references to the components we want to render dynamically. -
Iterating and Rendering: Using the
v-fordirective, we iterate over thecomponentsarray. For each component in the array, the<component>tag is used with the:isattribute to dynamically determine which component to render. -
Using the Main Component: Finally, we use the
DynamicListcomponent in our mainApp.vue.
This setup allows you to dynamically render a list of components in Vue 3 using the Composition API and TypeScript, taking advantage of the
<component>functionality to make your application more flexible and modular. -