How to use intellisense with at symbol ('@') in Vite?
Asked Answered
G

2

14

Using a Vite app I can include this in my vite.config.js:

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

which allows me to use the '@' symbol (at character) for path names. This lets me have imports that look like this:

import Home from "@/pages/Home.vue";

As opposed to this:

import Home from "../../../pages/Home.vue";

The problem is that intellisense won't show up in any meaningful way when using the '@' path but it will when I use the '..' path. How do I enable path intellisense starting with '@'

Pictures to clarify what I mean by "intellisense won't show up in any meaningful way when using the '@' path": Using '..' path

Using '@' path

Goering answered 26/8, 2022 at 2:56 Comment(0)
G
29

You also need to tell vscode with jsconfig.json or tsconfig.json file, for example:

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es6",
    "paths": {
      "@/*": ["./src/*"],
    }
  }
}
Grazing answered 26/8, 2022 at 3:1 Comment(3)
This does not work for me. I have it like this "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["resources/js/*"] } },Ambulant
@Ambulant make sure your jsconfig.json and resources folder are in a same directory, then it should work.Prominent
Yes, they are in the same directory but it still did not work. I ended up getting the correct answer from another question. #52432691Ambulant
D
0

You must add jsconfig.json file in root your project with paths parameter value:

"paths": {
  "@/*": [
    "./src/*"
  ]
},

jsconfig.json

structure of project

Decency answered 28/10, 2022 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.