[英]Vue cli 3 project alias src to @ or ~/ not working
I have installed the project with vue cli 3
but as the project grows, the import on the components are getting ugly, i end up importing a component like我已经使用
vue cli 3
安装了该项目,但是随着项目的增长,组件的导入变得越来越难看,我最终导入了一个类似的组件
import Component from '../../../../components/folder/Component.vue'
i just want to alias the src folder and do我只想给 src 文件夹取别名然后做
import Component from '@components/folder/Component.vue'
i did read that i have to modify the vue.config.js, i have done it but the error is the same我确实读到我必须修改 vue.config.js,我已经完成了但错误是一样的
Module not found: Error: Can't resolve '@components/Permissions/PermissionsTable'
this is my vue.config.js这是我的 vue.config.js
const path = require("path");
const vueSrc = "./src";
module.exports = {
runtimeCompiler: true,
css: {
modules: true
},
configureWebpack: {
resolve: {
alias: {
"@": path.join(__dirname, vueSrc)
}
}
}
};
am i missing something?我错过了什么吗? what else should i do?
我还应该做什么?
The complete example:完整示例:
const path = require("path");
const vueSrc = "./src";
module.exports = {
runtimeCompiler: true,
css: {
modules: true
},
configureWebpack: {
resolve: {
alias: {
"@": path.resolve(__dirname, vueSrc)
},
extensions: ['.js', '.vue', '.json']
}
}
};
Although I am not sure that extensions is the solution to the problem you had.尽管我不确定扩展是否可以解决您遇到的问题。 This will just add the extensions in case you haven't written those in the import definition:https://webpack.js.org/configuration/resolve/#resolveextensions
如果您没有在导入定义中编写扩展,这只会添加扩展:https ://webpack.js.org/configuration/resolve/#resolveextensions
I have a feeling it was rather a problem with the missing /
.我有一种感觉,这是缺少
/
的问题。 So instead of @components/Permissions/PermissionsTable
it should have been @/components/Permissions/PermissionsTable
because you did not add a /
at the end of your vueSrc
.因此,而不是
@components/Permissions/PermissionsTable
它应该是@/components/Permissions/PermissionsTable
因为您没有在vueSrc
的末尾添加/
。 So webpack will just attach it to ./src
like this: ./srccomponents/Permissions/PermissionsTable
.所以 webpack 会像这样将它附加到
./src
: ./srccomponents/Permissions/PermissionsTable
。
我缺少extensions: ['.js', '.vue', '.json'],
并且在导入中我必须使用 '@/components/...'
For Vue CLI you need to create a file in the root folder of project - jsconfig.json对于 Vue CLI,您需要在项目的根文件夹中创建一个文件 - jsconfig.json
{
"include": ["./src/**/*"],
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@/*": ["./*"]
}
},
"exclude": ["node_modules"]
}
and that's all, helped me with PhpStorm就是这样,帮助我使用 PhpStorm
You have你有
import Component from '@components/folder/Component.vue'
but you need但你需要
import Component from '@/components/folder/Component'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.