admin管理员组文章数量:1334292
I have a process which have many steps. Each one is implemented in a different component.
Initially I had this code:
<template>
<template v-if="process.currentStep === 0">
<Step0 />
</template>
<template v-else-if="process.currentStep === 1">
<Step1 />
</template>
<template v-else-if="process.currentStep === 2">
<Step2 />
:
:
</template>
<script setup>
import Step0 from "@/views/components/steps/step0.vue";
import Step1 from "@/views/components/steps/step1.vue";
import Step2 from "@/views/components/steps/step2.vue";
:
:
However, in order to make the code more readable, I tried to change to:
<template>
<component :is="`Step${process.currentStep}`" />
But it doesn't work.
I also tried:
<component :is="stepComponent" />
import { defineAsyncComponent } from 'vue';
const stepComponent = ref(); // Adjust the initial value as needed
const stepComponents = {
Step0: defineAsyncComponent(() => import('@/views/components/steps/step0.vue')),
Step1: defineAsyncComponent(() => import('@/views/components/steps/step1.vue')),
Step2: defineAsyncComponent(() => import('@/views/components/steps/step2.vue')),
};
But neither get any result.
I don't want to register these components in main.ts
. Is there any way to do what I am trying to do?
I have a process which have many steps. Each one is implemented in a different component.
Initially I had this code:
<template>
<template v-if="process.currentStep === 0">
<Step0 />
</template>
<template v-else-if="process.currentStep === 1">
<Step1 />
</template>
<template v-else-if="process.currentStep === 2">
<Step2 />
:
:
</template>
<script setup>
import Step0 from "@/views/components/steps/step0.vue";
import Step1 from "@/views/components/steps/step1.vue";
import Step2 from "@/views/components/steps/step2.vue";
:
:
However, in order to make the code more readable, I tried to change to:
<template>
<component :is="`Step${process.currentStep}`" />
But it doesn't work.
I also tried:
<component :is="stepComponent" />
import { defineAsyncComponent } from 'vue';
const stepComponent = ref(); // Adjust the initial value as needed
const stepComponents = {
Step0: defineAsyncComponent(() => import('@/views/components/steps/step0.vue')),
Step1: defineAsyncComponent(() => import('@/views/components/steps/step1.vue')),
Step2: defineAsyncComponent(() => import('@/views/components/steps/step2.vue')),
};
But neither get any result.
I don't want to register these components in main.ts
. Is there any way to do what I am trying to do?
- 1 Please don't tag your title question. See How to Ask. – isherwood Commented Nov 20, 2024 at 20:23
2 Answers
Reset to default 3is
value should be component object itself, it won't resolve a string like :is="'Step0'"
to a component automatically.
It is:
<script setup>
import Step0 from "@/views/components/steps/step0.vue";
...
const stepComponents = { Step0, ... }
...
<component :is="stepComponents[`Step${currentStep}`]"
Step0
, etc can be defineAsyncComponent
for lazy loading, but this is not necessary.
You need to use a computed properties which will act as component loader
<template>
<!-- Dynamically load the component based on the current step -->
<component :is="componentLoader" />
</template>
<script setup>
import { defineAsyncComponent, computed } from 'vue';
// Define async components for each step
const stepComponents = {
Step0: defineAsyncComponent(() => import('@/views/components/steps/step0.vue')),
Step1: defineAsyncComponent(() => import('@/views/components/steps/step1.vue')),
Step2: defineAsyncComponent(() => import('@/views/components/steps/step2.vue')),
};
// replace with your real process object
const process = reactive({
currentStep: 0,
});
// Compute the current component based on the current step
const componentLoader = computed(() => {
const stepKey = `Step${process.currentStep}`;
return stepComponents[stepKey] || null; // Return null if the step is not found
});
</script>
本文标签: javascriptHow can I render components dynamically with Vue 3Stack Overflow
版权声明:本文标题:javascript - How can I render components dynamically with Vue 3? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742332204a2454919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论