My current situation involves a template that loads dynamic components based on their names:
<template>
<div>
<div>
<div>
<component
:is="getFormRenderer"
></component>
</div>
</div>
</div>
</template>
The getFormRenderer
function retrieves a string (obtained from an API) which specifies the component by its name.
In this scenario, only two sub-components (telegram_send_message
and time_time
) are imported and registered:
<script>
import { useStore } from "vuex";
import { computed } from "vue";
import exampleComponentOne from "@/components/forms/exampleComponentOne.vue";
import exampleComponentTwo from "@/components/forms/exampleComponentTwo.vue";
import defaultComponentTwo from "@/components/forms/defaultComponent.vue";
export default {
name: "ActionEditor",
setup() {
const store = useStore();
const getFormRenderer = computed(() => {
return (
store.state.level.actionSelected.plugin
);
});
return {
getFormRenderer,
};
},
components: {
exampleComponentOne,
exampleComponentTwo,
defaultComponent
},
};
</script>
Now I am looking to enhance the functionality of the dynamic <component>
so that it defaults to defaultComponent.vue
if the specified component does not exist in the list.
I have explored using this.hasOwnProperty()
, but within the setup() function, this
is undefined. Is there a conventional method to accomplish this task?