Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

src alias in vite/vue project with @ symbol not working

I have installed Vue3/TS project with Vite CLI

My vite.config.ts looks like this:

import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
import path from 'path'

export default defineConfig({
    plugins: [vue()],

    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
        },
    },
})

Also I add 'paths' property inside tsconfig.json:

{
    "compilerOptions": {
        ...
        "baseUrl": "./",
        "paths": {
            "@/*": ["./src/*", "./dist/*"]
        }
    },
    "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}

With this setup '@' alias works fine with simple component importing. But in my case, I was needed in dynamic import with template strings:

<script setup lang="ts">
import { useStore } from '@/store/app'
import { computed, defineAsyncComponent } from 'vue'

const store = useStore()
const userRole = store.getUserRole

const component = computed(() => {
    return defineAsyncComponent(
        () => import(`@/components/pages/dashboard/${userRole}.vue`)
    )
})
</script>

This sample throw an error:

Uncaught (in promise) TypeError: Failed to resolve module specifier '@/components/pages/dashboard/admin.vue' at dashboard.vue:14:54

If I replace '@' with dot-notation - it works fine. Need your help)

like image 512
Kirill Galimov Avatar asked Oct 27 '25 23:10

Kirill Galimov


1 Answers

Try like this:

resolve: {
  alias: {
    '@': path.resolve(__dirname, 'src'),
  }
}
like image 149
Nikola Pavicevic Avatar answered Oct 29 '25 14:10

Nikola Pavicevic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!