我从 Angular 8.2 升级到 Angular 9。我的项目正在按预期工作,构建也是如此,但是许多测试都因此错误而失败:
TypeError: Cannot read property 'ngModule' of undefined
at isModuleWithProviders (../packages/core/src/render3/jit/module.ts:530:37)
at expandModuleWithProviders (../packages/core/src/render3/jit/module.ts:523:7)
at Array.map (<anonymous>)
at Function.get (../packages/core/src/render3/jit/module.ts:127:29)
at getNgModuleDef (../packages/core/src/render3/definition.ts:761:27)
at isNgModule (../packages/core/src/render3/jit/module.ts:534:12)
at ../packages/core/src/render3/jit/module.ts:464:10
at Array.forEach (<anonymous>)
at transitiveScopesFor (../packages/core/src/render3/jit/module.ts:458:30)
at setScopeOnDeclaredComponents (../packages/core/src/render3/jit/module.ts:388:28)
at Object.flushModuleScopingQueueAsMuchAsPossible [as ɵflushModuleScopingQueueAsMuchAsPossible] (../packages/core/src/render3/jit/module.ts:59:11)
at TestBedRender3.flushModuleScopingQueueAsMuchAsPossible [as checkGlobalCompilationFinished] (../../packages/core/testing/src/r3_test_bed.ts:400:7)
at TestBedRender3.Object.<anonymous>.TestBedRender3.resetTestingModule (../../packages/core/testing/src/r3_test_bed.ts:231:10)
at Function.Object.<anonymous>.TestBedRender3.resetTestingModule (../../packages/core/testing/src/r3_test_bed.ts:180:26)
at ../../packages/core/testing/src/before_each.ts:25:13
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:386:30)
at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:117:43)
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:385:36)
at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:143:47)
这是一个失败的非常简单的测试的示例:
import { ExampleModule } from './example.module';
describe('ExampleModule', () => {
let exampleModule: ExampleModule;
beforeEach(() => {
exampleModule = new ExampleModule();
});
it('should create an instance', () => {
expect(exampleModule).toBeTruthy();
});
});
如有任何帮助,我们将不胜感激。谢谢。
就我而言,这是关于我需要在其他模块中导入的组件/模块的导出。所以问题不在于进口,而在于出口。而不是:
export { FirstComponent } from './first.component';
export { FirstModule } from './first.module';
我需要将其导出为:
export * from './first.component';
export * from './first.module';
在 Angular 13 中不再有
TypeError: Cannot read property 'ngModule' of undefined
。
这最终成为桶进口的问题。为了解决这个问题,我必须删除所有桶导入并深度导入所有文件。
旧的例子:
import {
Example1,
Example2
} from 'path/to/example-folder';
新示例:
import { Example1 } from 'path/to/example1-file';
import { Example2 } from 'path/to/example2-file';
我也遇到了同样的问题,结果发现根本原因在于循环依赖,根据堆栈跟踪,这不是很直观。解决循环导入解决了这个问题。
Angular 的存储库中有一个原始步骤,其中包含更多详细信息 https://github.com/angular/angular/issues/39573