Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Load fallback component if main component does not exits #13058

Unanswered
opuu asked this question in Help/Questions
Discussion options

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 website:

[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?

You must be logged in to vote

Replies: 2 comments

Comment options

Found a way to get rid of the error, but still interested if there is any better way.

<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>
You must be logged in to vote
0 replies
Comment options

You can simplify this by using the errorComponent option in defineAsyncComponent:

<template>
 <MainFooter></MainFooter>
</template>
<script lang="ts" setup>
import { defineAsyncComponent } from 'vue';

const modules = import.meta.glob('@wtp/MainFooter.vue');

const MainFooter = defineAsyncComponent({
 loader: (): any => modules['@wtp/MainFooter.vue']?.() || Promise.reject(),
 errorComponent: () => 'error',
});
</script>
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /