1

I was wondering if there was a way to use a Component as a property in Vue 3.

Take for example the below TS interface:

import type { Component } from 'vue'

interface Route {
    url: string
    icon: Component
    name: string
}

Can I use an object based on said interface in a template to render a Component like this?

<div v-for="route in routes">
{{ route.icon }} // not working.
</div>

Any help would be much appreciated. Thanks!

3
  • how do you get the routes variable? Commented Feb 6, 2023 at 12:08
  • @AlexandreHeinen it's an array of objects I defined based on the interface routes = [ {name: "Index", url: "/", icon: SomeVueComponent},] Commented Feb 6, 2023 at 12:15
  • can you provide the code where you create this array? Commented Feb 6, 2023 at 13:00

1 Answer 1

2

You want to use dynamic components.

It's a special component that take a registered component's name or an entire imported component object as a prop:

<template>
  <component :is="route.icon" />
</template>

<script setup>
import Header from './Header.vue'

const globalComponentName = 'MyPopup'

const route = reactive({
  icon: 'MyPopup',
  // or
  icon: Header
})
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.