foreword
During the project development process, when importing the project .tsx
into the page, the file reports an error: The module "@/xxx/xxx" or its corresponding type declaration cannot be found. The main reason is that currently the prompt for the directory pointed to by is not supported, and vite does not support it by default. So you need to manually configure the pointing of the symbolimport
.ts
ts
@
src
@
Configuration of road king aliases
Add configuration in vite.config.ts
:
import path from "path"
export default defineConfig({
...
plugins: [react()],
resolve: {
alias: {
"@": path.resolve(__dirname, './src')
}
}
})
At this time, the imported path
module will report red, but we already have node
, path
the module exists, but ts
some declaration configurations of are missing, so just install the declaration configuration about node
this libraryts
npm i -D @types/node
If the installation is successful, there will be no red flags. If import
the following red flags are red, just path
replace the import withimport * as path from 'path';
Tips for configuring path aliases
Although the alias of Road King already exists, @
there is no prompt for Road King when entering in the file.
We need tsconfig.json
to add two configurations in :
"compilerOptions": {
...
"baseUrl": "./",
"paths": {
"@/*": [
"src/*"
]
}
},
After the configuration is completed, @
there will be a road resource prompt after writing.