简体   繁体   English

tsconfig.app.json 的别名不起作用 Angular 11

[英]Aliasing for tsconfig.app.json not working Angular 11

I'm having an issue getting aliasing to work since I've moved my app into an angular project自从我将应用程序移入 angular 项目后,我在使用别名时遇到问题

None of my imports work with my "@app" alias我的所有导入都不适用于我的“@app”别名

mainApp
│   package.json
│   tsconfig.json
│   angular.json    
│
└───projects
    │  
    └───my-project (That I need the aliasing for)
        │  tsconfig.app.json
        │ 
        └───src
             │  
             └───app

When I use:当我使用:

import { MyService } from '@app/shared/services/my.service';

I get errors我收到错误

In my tsconfig.app.json:在我的 tsconfig.app.json 中:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "baseUrl": "./",
    "paths": {
      "@app/*": ["src/app/*"],
      "@env/*": ["environments/*"]
    },
  "include": [
    "src/**/*.d.ts"
  ]
}

I've tried我试过了

"@app/*": ["projects/my-project/src/app/*"],

I also updated my main tsconfig.json:我还更新了我的主要 tsconfig.json:

....
"baseUrl": "./",
  "paths": {
    "@app/*": ["src/app/*"],
    "@env/*": ["environments/*"]
  },
...

And then serve the project:然后为项目服务:

ng serve --project my-project

The issue was because my paths object was outside of the compilerOptions object, so:问题是因为我的路径 object 在compilerOptions object 之外,所以:

 "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@app/*": ["projects/my-/src/app/*"],
      "@env/*": ["environments/*"]
    },
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  }

worked!!!工作! Rather than而不是

 "compilerOptions": {
.......
},"paths": {
      "@app/*": ["projects/my-project/src/app/*"],
      "@env/*": ["environments/*"]
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 路径在 tsconfig.app.json 中没有按预期工作 - paths is not working in tsconfig.app.json as expected Angular 9 tsconfig.json & tsconfig.app.json 路径未按预期工作 - Angular 9 tsconfig.json & tsconfig.app.json paths are not working as expected 使用tsconfig.app.json - Use of tsconfig.app.json ENOENT:在使用 angular 4 的 tsconfig.app.json 中没有这样的文件或目录? - ENOENT: no such file or directory in tsconfig.app.json using angular 4? 在 tsconfig.app.json 中为 angular 项目定义路径 - define path in tsconfig.app.json for angular project Angular 中 tsconfig.json 和 tsconfig.app.json 文件的区别 - Difference between tsconfig.json and tsconfig.app.json files in Angular 角度5:在安装aws-sdk时,在tsconfig.app.json中删除“类型”:[]或更改为“类型”:[“节点”]后,IE无法工作 - Angular 5: IE not working after removing “types”: [ ] or changing to “types”: [“node”] in tsconfig.app.json during installing of aws-sdk 更改tsconfig.app.json的位置 - Change the location of tsconfig.app.json tsconfig.app.json中的baseUrl不会覆盖tsconfig.json中的baseUrl - baseUrl in tsconfig.app.json is not overiding the baseUrl in tsconfig.json 部署到App Engine时找不到tsconfig.app.json - tsconfig.app.json not found when deploying to App Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM