Configure base URL in Vite – Vite

by
Ali Hasan
laravel-vite node.js vuejs3

Quick Fix: In Vue 3, to dynamically import components, you need to use defineAsyncComponent(() => import('./component.vue')). The old syntax import('./component.vue') from Vue 2 is no longer supported.

The Problem:

You are working on a Vue 3 application built with Vite. You want to specify the base URL from which Vite should load files. The server root path is /app/, but Vite is assuming it’s in the root path, resulting in incorrect lazy-loaded Vue view loading. How can you configure Vite to use the correct base URL?

The Solutions:

Solution 1: Use DefineAsyncComponent

The issue mentioned in the problem statement is related to Vue 3 and dynamic imports. To resolve it, instead of using a direct import statement for lazy-loading Vue views, you need to use the defineAsyncComponent function provided by Vue 3. Here’s how you can modify your code:

import { defineAsyncComponent } from 'vue';

public glyph = () => defineAsyncComponent(() => import('./GlyphCard.vue'));

Q&A

How can I tell Vite where to load my files from?

Edit the resolve.alias in your Vite config file.

Did not relate to Vite?

The specific issue was related to dynamic imports in Vue 3.

Is there a resource for more information?

Video Explanation:

The following video, titled "Build a Chat App with NEW ChatGPT API | Full stack, React, Redux ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... vite: https://vitejs.dev/guide/ react router: https ... path=/docs/multichatwindow--default-story chatengine api: https://rest ...