【问题标题】:Paths in tsconfig not found未找到 tsconfig 中的路径
【发布时间】:2019-12-10 20:20:41
【问题描述】:

我在同一个 Angular 8 应用中有多个项目。 在“root”tsconfig.json 我为@models 添加路径

"compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "dom"],
    "paths": {
      "@models": ["projects/shared/models"]    <=== this
    }
  },

我从index.ts桶导入

export * from "./users/user-profile.model";
export * from "./users/users.model";

当我尝试从@models 导入任何组件模型时

import { IUser } from "@models";

我收到错误

错误 TS2307:找不到模块“@models”

但是当我尝试直接从桶中导入它时,工作正常

在我的每个项目中,我也有tsconfig.app.json,但我只是将路径添加到根目录

【问题讨论】:

  • "@models": ["projects/shared/models"] 应该在 tsconfig.json 文件中替换为 "@models": ["projects/shared/models/index.ts"]

标签: angular typescript


【解决方案1】:

问题出在 VS 代码中,多次重启后一切正常

【讨论】:

    【解决方案2】:

    "@models": ["projects/shared/models"] 应该在 tsconfig.json 文件中替换为 "@models": ["projects/shared/models/index.ts"] 或者你也可以像下面这样写

      "@models": [
        "projects/shared/models/*"
      ],
    

    【讨论】:

    • 能否分享一个 stackblitz 链接以供进一步调查?
    • 我会尝试,但这是一个非常大的应用程序,我尝试做例子
    • 你能分享一下tsconfig.app.json吗?请检查 tsconfig.app.json 中设置的基本 url 是什么
    • 这是 tsconfig.app.json { "extends": "../../tsconfig.json", "compilerOptions": { "outDir": "../../out -tsc/app", "types": [] }, "files": ["src/main.ts", "src/polyfills.ts"], "include": ["src/**/*.ts "], "排除": ["src/test.ts", "src/**/*.spec.ts"] }
    • 我在本地尝试过,它可以工作。请尝试重新启动您的编辑器。
    【解决方案3】:

    试试这个

    "paths": {
          "@models/*": ["projects/shared/models/*"]
        }
    

    更新tsconfig.json后,重启应用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-08
      • 2020-12-05
      • 2018-11-13
      • 2019-01-28
      • 1970-01-01
      • 2019-07-20
      • 2022-01-12
      • 2020-12-23
      相关资源
      最近更新 更多