1、第一种
1、vscode安装 Path Intellisense插件
2、打开设置 - 首选项 - 搜索 Path Intellisense
- 打开 settings.json
,添加
"path-intellisense.mappings": {
"@": "${workspaceRoot}/src"
}
3、在项目 package.json
所在同级目录下创建文件 jsconfig.json
:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
}
},
"exclude": [
"node_modules"
]
}
2、第二种
1、安装 path
:
yarn add path
# 或者npm
npm i path -S
2、创建 vue.config.js
:
const path = require("path");
function resolve(dir) {
return path.join(__dirname, dir);
}
module.exports = {
chainWebpack: config => {
config.resolve.alias
.set("@", resolve("src"))
.set("assets", resolve("src/assets"))
.set("components", resolve("src/components"))
.set("base", resolve("baseConfig"))
.set("public", resolve("public"));
},
}