Utilizing Vue3's Component
Type:
Take advantage of Vue's Component
type, ensuring that your interface is actively utilized. Moreover, ensure that the references to MyComponentList point towards a registered component.
<script setup lang="ts">
import type { Component } from 'vue'
import MyComponentA from "@/components/MyComponentA.vue"
import MyComponentB from "@/components/MyComponentB.vue"
import MyComponentC from "@/components/MyComponentC.vue"
interface IComponents {
[key: string]: Component
}
// Create a list of component items
const MyComponentList: IComponents = {
// Keys can potentially be without quotes, but this approach is more explicit and less confusing.
"MyComponentA": MyComponentA,
"MyComponentB": MyComponentB,
"MyComponentC": MyComponentC
}
</script>
Subsequently, within your template, specify a dynamic component name (SomeString
) that should correspond to any key in MyComponentList
, like "MyComponentA":
<component :is="MyComponentList[SomeString as keyof typeof MyComponentList]" />