我在Vite.js上使用Vue 3和Eslint + Airbnb配置。Airbnb有一个规则eslint(import/no-unresolved)
,这很好,但是Eslint不知道如何解析别名路径。
我想为路径使用别名-例如:import TableComponent from '@/components/table/TableComponent.vue'˙
环境位于平原JavaScript。
我成功地设置了我的vite.config.js
,以便应用程序能够像这样解析路径:
import path from 'path';
import { defineConfig } from 'vite';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: [{
find: "@", replacement: path.resolve(__dirname, 'src')
},],
},
});
Vue应用程序是这样工作的,并且正确地解析了导入路径,但是Eslint一直报告错误:Unable to resolve path to module eslint(import/no-unresolved)
我怎么告诉埃斯林特如何解决化名?
我试过了:install eslint-plugin-import eslint-import-resolver-alias --save-dev
// .eslintrc.js
// ...
extends: [
'eslint:recommended',
'plugin:import/recommended',
'airbnb-base',
'plugin:vue/vue3-strongly-recommended',
],
settings: {
'import/resolver': {
alias: {
map: [
['@', 'src'],
],
},
},
},
但这不管用。
编辑:
解决了这个问题,如果您像我一样使用普通的JavaScript,请参阅已接受的答案。
如果您正在使用TypeScript,请查看赛义德的回答是否可以帮助您。
发布于 2021-06-05 08:42:14
如果有人遇到这个问题,这在我的情况下是可行的*
settings: {
'import/resolver': {
alias: {
map: [
['@', './src'],
],
},
},
},
*在我的例子中,Vue的根目录是'src‘目录,而Eslint的根目录更高,所以它需要'./src’路径。
非常感谢@https://github.com/aladdin-add通过github问题给出的答案!
发布于 2021-10-20 05:41:17
这解决了我的TypeScript
项目中的问题。
npm install eslint-import-resolver-typescript
安装eslint-import-resolver-typescript
后
{
// other configuration are omitted for brevity
settings: {
"import/resolver": {
typescript: {} // this loads <rootdir>/tsconfig.json to eslint
},
},
}
应该添加到.eslintrc.js.
中
我的tsconfig.json (删除不需要的设置)
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"strict": false,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"types": ["vite/client", "node"],
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
"allowJs": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"exclude": ["node_modules"]
}
查看讨论这里
发布于 2021-07-09 11:16:00
我也有同样的问题,即使我修复了src路径--它仍然是问题所在。直到我添加了扩展之后,它才起作用:
"settings": {
"import/resolver": {
"alias": {
"map": [
["@", "./src"]
],
"extensions": [".js",".jsx"] <--- HERE
}
}
},
https://stackoverflow.com/questions/67835072
复制相似问题