admin管理员组文章数量:1384310
The MainFooter might not exist in the themes folder, in that case we are loading fallback component.
Here is what I came up with:
<template>
<component :is="footerComponent" />
</template>
<script lang="ts" setup>
const footerComponent = computed(() => {
try {
return import("@wtp/MainFooter.vue");
} catch (error) {
console.error(error);
return defineAsyncComponent(() => import("./FallbackFooter.vue"));
}
});
</script>
This works but gives the following error on frontend:
[plugin:vite:import-analysis] Failed to resolve import "@wtp/MainFooter.vue" from "components/MainFooter.vue". Does the file exist?
Any better ways to do this?
The MainFooter might not exist in the themes folder, in that case we are loading fallback component.
Here is what I came up with:
<template>
<component :is="footerComponent" />
</template>
<script lang="ts" setup>
const footerComponent = computed(() => {
try {
return import("@wtp/MainFooter.vue");
} catch (error) {
console.error(error);
return defineAsyncComponent(() => import("./FallbackFooter.vue"));
}
});
</script>
This works but gives the following error on frontend:
[plugin:vite:import-analysis] Failed to resolve import "@wtp/MainFooter.vue" from "components/MainFooter.vue". Does the file exist?
Any better ways to do this?
Share Improve this question edited Mar 17 at 22:27 desertnaut 60.5k32 gold badges155 silver badges181 bronze badges asked Mar 17 at 22:01 Obaydur RahmanObaydur Rahman 8621 gold badge7 silver badges20 bronze badges 3 |1 Answer
Reset to default 1Managed to solve the issue, by using import.meta.glob
to check if the component exists.
<template>
<component :is="footerComponent" />
</template>
<script lang="ts" setup>
const modules = import.meta.glob("@wtp/MainFooter.vue");
const footerComponent = shallowRef();
if (modules["@wtp/MainFooter.vue"]) {
// @ts-ignore
footerComponent.value = defineAsyncComponent(() => modules["@wtp/MainFooter.vue"]());
} else {
footerComponent.value = defineAsyncComponent(() => import("./FallbackFooter.vue"));
}
</script>
本文标签: vuejsLoad fallback component if main component does not exitsStack Overflow
版权声明:本文标题:vue.js - Load fallback component if main component does not exits - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744533153a2611154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
import.meta.glob
, this might not be the best solution but it is working. – Obaydur Rahman Commented Mar 18 at 20:22