【问题标题】:Can't resolve all parameters for ApplicationModule: (?)无法解析 ApplicationModule 的所有参数:(?)
【发布时间】:2018-12-24 12:38:52
【问题描述】:

我刚刚将一个应用程序模块迁移为可导入库。

我正在尝试使测试正常工作,就像以前一样,但我收到此错误:

Error: Can't resolve all parameters for ApplicationModule: (?).
at syntaxError (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:1275:17)
at CompileMetadataResolver._getDependenciesMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:11176:35)
at CompileMetadataResolver._getTypeMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:11069:26)
at CompileMetadataResolver.getNgModuleMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10937:24)
at CompileMetadataResolver.getNgModuleSummary (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10747:35)
at eval (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10861:51)
at Array.forEach (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10849:49)
at CompileMetadataResolver.getNgModuleSummary (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10747:35)
at eval (webpack:///C:/Repositories/MyProject/my-library/node_modules/@angular/compiler/fesm5/compiler.js?:10861:51)

我已经看到了这些可能的解决方案: Error: Can't resolve all parameters for ApplicationModule: (?)Cannot resolve parameters for ApplicationModule: (?) 但他们无法解决我的问题。

projects/my-library/src/test.ts

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

如果您需要更多信息来找出问题所在,请告诉我。

【问题讨论】:

  • 您的应用程序模块中有什么?可以发一下吗?
  • 没有Application Module。我发现了错误是什么,我将在答案中发布它。

标签: angular testing karma-jasmine


【解决方案1】:

最后,我找到了问题所在,我很难找到它,因为它不相关。问题来自这个 tslint 配置:

tslint.json

...
"ordered-imports" [
  true,
  { "named-imports-order": "lowercase-last" }
]
...

即使配置只有"ordered-imports": true,当ng lint 启动时,它也会导致test.ts 文件出现问题,因此我更改了该文件以满足 tslint 要求:

test.ts

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import 'core-js/es7/reflect'; // <-- Moved from the top of imports
import 'zone.js/dist/zone'; // <-- Moved from the top of imports
import 'zone.js/dist/zone-testing'; // <-- Moved from the top of imports

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

这个文件导致了我得到的错误。该文件必须如下所示:

正确的test.ts

import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

【讨论】:

    【解决方案2】:

    对于 Angular 7 及更高版本,请尝试在 polyfill.ts 文件中添加:

    import "core-js/features/reflect";

    【讨论】:

    • 多一点解释将有助于使这个答案更有用 - 并且更有可能得到你的支持
    • @RubenHelsloot 路径在最新版本的 core-js (3.0.1) 中发生了变化。如果您使用 polyfilling 然后升级您的版本,就会发生此问题。
    【解决方案3】:

    您的应用程序模块中有一个您没有以任何方式提供或导入的依赖项。您可以通过查看您的 ApplicationModule 构造函数方法来确定哪个 - 它可能注入了一些服务或类似的东西,而您没有在 imports 元数据的 imports 数组中提供它。

    【讨论】:

      【解决方案4】:

      在 polifills.ts 文件中添加这个

       import 'core-js/es7/reflect';
      

      【讨论】:

      • 多一点解释将有助于使这个答案更有用 - 并且更有可能得到你的支持
      猜你喜欢
      • 2019-04-25
      • 2019-03-25
      • 1970-01-01
      • 2019-09-17
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      相关资源
      最近更新 更多