gpt4 book ai didi

angular - typescript /TSLint : TSLint not recognizing root-relative imports via baseUrl

转载 作者:太空狗 更新时间:2023-10-29 17:33:26 27 4
gpt4 key购买 nike

根据 this solution,我通过 tsconfig.json 中的 baseUrl 编译器选项使用根目录相对导入,但我遇到了一个问题,Atom IDE 向我显示如下所示的 linting 错误:

Cannot find module 'core/nav-menu/nav-menu.component'.

导入看起来像(在 src/app/core/nav-menu.module.ts 中):

import { NavMenuComponent } from 'core/nav-menu/nav-menu.component';

Atom 中的 TSLint 没有找到与根目录相关的导入文件,但 Angular 编译器对它们没有问题。

我不确定我的 tslint.json 配置是否错误...在我在同一台机器上的另一个项目中,一切都按预期工作,但我无法发现这个项目有什么不同,导致了这个问题。

tslint.json:

{
"rulesDirectory": [
"node_modules/codelyzer"
],
"rules": {
"arrow-return-shorthand": true,
"callable-types": true,
"class-name": true,
"comment-format": [
true,
"check-space"
],
"curly": true,
"deprecation": {
"severity": "warn"
},
"eofline": true,
"forin": true,
"import-blacklist": [
true,
"rxjs/Rx"
],
"import-spacing": true,
"indent": [
true,
"spaces"
],
"interface-over-type-literal": true,
"label-position": true,
"max-line-length": [
true,
140
],
"member-access": false,
"member-ordering": [
true,
{
"order": [
"static-field",
"instance-field",
"static-method",
"instance-method"
]
}
],
"no-arg": true,
"no-bitwise": true,
"no-console": [
true,
"debug",
"time",
"timeEnd",
"trace"
],
"no-construct": true,
"no-debugger": true,
"no-duplicate-super": true,
"no-empty": false,
"no-empty-interface": true,
"no-eval": true,
"no-inferrable-types": [
true,
"ignore-params"
],
"no-misused-new": true,
"no-non-null-assertion": true,
"no-shadowed-variable": true,
"no-string-literal": false,
"no-string-throw": true,
"no-switch-case-fall-through": true,
"no-trailing-whitespace": true,
"no-unnecessary-initializer": true,
"no-unused-expression": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"object-literal-sort-keys": false,
"one-line": [
true,
"check-open-brace",
"check-catch",
"check-else",
"check-whitespace"
],
"prefer-const": true,
"quotemark": [
true,
"single"
],
"radix": true,
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"unified-signatures": true,
"variable-name": false,
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
],
"no-output-on-prefix": true,
"use-input-property-decorator": true,
"use-output-property-decorator": true,
"use-host-property-decorator": true,
"no-input-rename": true,
"no-output-rename": true,
"use-life-cycle-interface": true,
"use-pipe-transform-interface": true,
"component-class-suffix": true,
"directive-class-suffix": true
}
}

src/tslint.json:

{
"extends": "../tslint.json",
"rules": {
"directive-selector": [
true,
"attribute",
"s2es",
"camelCase"
],
"component-selector": [
true,
"element",
"s2es",
"kebab-case"
]
}
}

tsconfig.json:

{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "app",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}

src/tsconfig.app.json:

{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"types": [],
"baseUrl": "app",
"types": ["node"]
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}

附带说明一下,为什么 baseUrl: "app" 不是默认值?为什么文件相对导入看起来像是 Angular/TypeScript 开发的标准。太疯狂了。

最佳答案

您的基本网址错误:

您的 tsconfig.json 必须设置为:"baseurl": "./"

您的 tsconfig.app.ts 中不需要 baseurl,应该将其删除。

正如评论中所建议的,导入路径也应该更新。在导入路径前添加'src/app'。

如果你的结构如下并且你运行的是 Angular 6,这适用:

 * project
| - src
| | - app (with app files like app.modules.ts)
| | - tsconfig.app.json
| - tsconfig.json

也许您在试图找出问题所在时篡改了文件。这也是新生成的 Angular 项目的标准配置。

关于angular - typescript /TSLint : TSLint not recognizing root-relative imports via baseUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51292139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com