简体   繁体   English

Angular 和 Typescript 更新后找不到命名空间“MyApp”

[英]Cannot find namespace 'MyApp' after Angular and Typescript update

I have a type definition file where all dto objects for my app are exposed to global scope.我有一个类型定义文件,其中我的应用程序的所有 dto 对象都暴露在全局范围内。

DtosGenerator.d.ts DtosGenerator.d.ts

declare module MyApp.Rest.Dto {
   interface IAJsonDto {
   }
 }
 declare module MyApp.Rest.Dto.Post {
   interface ITest1PostDto extends MyApp.Rest.Dto.IAJsonDto {
    code: string;
 }
interface ITest2PostDto extends MyApp.Rest.Dto.IAJsonDto {
    id: number;
 }
interface ITest3PostDto extends MyApp.Rest.Dto.IAJsonDto {
    id: number;
}
  ....
}

And consumed in the following way.并按以下方式食用。

file1.service.ts文件1.service.ts

import ITest3PostDto = MyApp.Rest.Dto.Post.ITest3PostDto;

My app is build on Angular framework and I have just completed the migration from Angular(8), Typescript(3.5.3) to Angular9,Typescript(3.6.4) respectively.我的应用程序是建立在 Angular 框架上的,我刚刚分别完成了从 Angular(8), Typescript(3.5.3) 到 Angular9,Typescript(3.6.4) 的迁移。 Before migration the consumption of the Dto objects produced no errors and everything seemed normal.在迁移之前,Dto 对象的消耗没有产生错误,一切似乎都很正常。 After the migration, when the app is built on watch mode running the迁移后,当应用程序在手表模式下运行时

ng build --extractCss=true --watch command, the console outputs 'Cannot find namespace 'MyApp'. ng build --extractCss=true --watch命令,控制台输出'Cannot find namespace 'MyApp'。 When I add当我添加

/// <reference path="../../../../DtosGenerator.d.ts" /> 

on top of the file the error is produced no more.在文件顶部不再产生错误。 Is there a way to keep my code as it was before the migration without adding the reference path.有没有办法在不添加引用路径的情况下保持我的代码在迁移之前的状态。

Could anybody tell me what goes wrong?有人能告诉我出了什么问题吗?

For anyone who faces of will face the same issue I found the solution.对于任何面临相同问题的人,我找到了解决方案。 After hours of researching I found out that Angular automatic update ( https://update.angular.io/ ) changed tsconfig.app.json file configuration.经过数小时的研究,我发现 Angular 自动更新( https://update.angular.io/ )更改了 tsconfig.app.json 文件配置。

tsconfig.app.json before migration迁移前的 tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
  "outDir": "../out-tsc/app",
  "types": [ "node" ],
  "typeRoots": [
     "node_modules/@types"
  ]
},
"exclude": [
  "test.ts",
  "**/*.spec.ts"
]
}

tsconfig.app.json after migration:迁移后的 tsconfig.app.json:

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

The problem is that before migration all files were included in the compilation process and after it only the 2 files specified in the "files" array and all type definition files inside src directory are included.问题是,在迁移之前,所有文件都包含在编译过程中,之后只有“files”数组中指定的 2 个文件和 src 目录中的所有类型定义文件都包含在内。 'DtosGenerator.d.ts' file is located in the root of src folder so some mofications needed to be done to produce the final correct tsconfig.app.json file. 'DtosGenerator.d.ts' 文件位于 src 文件夹的根目录中,因此需要进行一些修改以生成最终正确的 tsconfig.app.json 文件。

tsconfig.app.json final tsconfig.app.json 最终

{
   "extends": "../tsconfig.json",
   "compilerOptions": {
   "outDir": "../out-tsc/app",    
   "typeRoots": [
      "node_modules/@types"      
   ]
},
  "files": [
    "main.ts",
    "polyfills.ts"
  ],
  "include": [
     "*.d.ts"
  ]
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM