TypeScript/TSLint:TSLint无法通过baseUrl识别根相对导入

10
我正在使用根相对导入,通过在 tsconfig.json 中使用 baseUrl 编译器选项,根据 这个解决方案,但我遇到了一个问题,Atom IDE 显示了类似以下的错误提示信息:

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开发的标准?这太疯狂了。

在你的项目中,这个组件的引用在哪里 Cannot find module 'core/nav-menu/nav-menu.component.. 分享一下代码。 - eduPeeth
好的,我编辑了一个导入示例。每个根相对导入都会出现相同的错误。 - BBaysinger
你尝试设置 rootDirs 设置了吗?那可能会有所帮助。 - David
这不是tslint的问题。 - Aluan Haddad
1个回答

8

您的基础URL不正确:

您的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项目的标准配置。

这是正确的。如果您的基本URL实际上是app,那么您应该将rootDir设置为相同的。 - Aluan Haddad
1
是的。这绝对有效。然后我不得不将src/app/添加到所有根相对导入中。所以,我在https://dev59.com/21sW5IYBdhLWcg3wwZfG上使用的解决方案实际上并不是一个理想的解决方案,尽管我在另一个项目中可以正常工作而没有错误。(我不知道有什么不同让它能够工作)。这是更好的解决方案,因为根相对导入可以很容易地被识别和通过查询src/app/轻松找到/搜索。我将这个解决方案添加到我找到之前配置的答案的问题中。 - BBaysinger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接