【发布时间】:2021-04-18 22:32:38
【问题描述】:
升级到 Angular 11 后,我无法再 ng serve 我的 Web 应用程序了。
我正在使用 Spring Doc 和最新的 OpenAPI 生成器 gradle-plutin (5.0.0) 生成客户端。
问题似乎与我的(生成的)REST 客户端有关。打开 https://localhost:4200 会将以下内容写入控制台:
main.ts:12 Error: inject() must be called from an injection context
at injectInjectorOnly (core.js:4901) [angular]
at Module.ɵɵinject (core.js:4911) [angular]
at Object.ApiModule_Factory [as factory] (meditation-rest-client.js:2885) [angular]
at R3Injector.hydrate (core.js:11158) [angular]
at R3Injector.get (core.js:10979) [angular]
at :4200/vendor.js:82591:55 [angular]
at Set.forEach (<anonymous>) [angular]
at R3Injector._resolveInjectorDefTypes (core.js:11016) [angular]
at new NgModuleRef$1 (core.js:25046) [angular]
at NgModuleFactory$1.create (core.js:25100) [angular]
at :4200/vendor.js:100468:45 [angular]
at Object.onInvoke (core.js:28301) [angular]
以下是我的tsconfig.json文件:
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"module": "es2020",
"lib": [
"es2018",
"dom"
]
}
}
这就是我在package.json中定义依赖的方式
"chart.js": "^2.9.4",
"core-js": "^3.8.2",
"meditation-rest-client": "./generated/meditation-rest-client",
"http-server": "^0.12.3",
"http-status-codes": "^2.1.4",
"moment": "^2.29.1",
这是上面错误消息中提到的那些生成的行:
// ...
ApiModule.ɵmod = ɵngcc0.ɵɵdefineNgModule({ type: ApiModule });
ApiModule.ɵinj = ɵngcc0.ɵɵdefineInjector({ factory: function ApiModule_Factory(t) { return new (t || ApiModule)(ɵngcc0.ɵɵinject(ApiModule, 12), ɵngcc0.ɵɵinject(ɵngcc1.HttpClient, 8)); }, providers: [], imports: [[]] });
ApiModule.ctorParameters = () => [
{ type: ApiModule, decorators: [{ type: Optional }, { type: SkipSelf }] },
{ type: HttpClient, decorators: [{ type: Optional }] }
];
// ...
这曾经在 Angular 9 中工作,但在 Angular 11 下无法使用。
知道如何解决这个问题吗?我已经尝试按照建议的here 和here 设置"preserveSymlinks": true。
如果您需要更多信息,请告诉我。
【问题讨论】:
标签: angular openapi openapi-generator springdoc