繁体   English   中英

错误:StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]:(NullInjectorError:没有路由器的提供者!)

[英]Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: (NullInjectorError: No provider for Router!)

我正在为 AppComponent 准备单元测试用例,它有路由器作为注入的依赖项,并在我的测试台中包含了 RouterTestingModule。 但仍然收到一个奇怪的错误。 请找到如下所示的错误日志:

Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: 
  StaticInjectorError(Platform: core)[RouterLinkWithHref -> Router]: 
    NullInjectorError: No provider for Router!

错误属性:对象({ ngTempTokenPath:null,ngTokenPath:['RouterLinkWithHref',Function],ngDebugContext:DebugContext_({视图:对象({ def:对象({工厂:函数,nodeFlags:671753,rootNodeFlags:1,nodeMatchedQueries:0 , flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 1, childFlags: 671753, directChildFlags: 1, childMatchedQueries: 0 ,matchedQueries:Object({}),matchedQueryIds:0,references:Object({}),ngContentIndex:null,childCount:10,bindings:[Object({flags:8,ns:'',name:'className', nonMinifiedName: 'className', securityContext: 0, suffix: undefined }) ], bindingFlags: 8, 输出: [ ], element: Object({ ns: '', name: 'nav', attrs: [ Array ], template: null, componentProvider: null, componentView: null, componentRendererType: null, publicProviders: null({ }), allProviders: null({ }), handleEvent: Function }), provider: null, text: null, query: null, ngContent:空值 }), Object({ ... Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: StaticInjectorError(Platform: core)[RouterLinkWithHref -> Router]: NullInjectorError: No provider for Router! 在 NullInjector.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:1360:19) 在 resolveToken (webpack:///./node_modules/@angular/core/fesm5/core .js?:1598:24) at tryResolveToken (webpack:///./node_modules/@angular/core/fesm5/core.js?:1542:16) at StaticInjector.get (webpack:///./node_modules/ @angular/core/fesm5/core.js?:1439:20) at resolveToken (webpack:///./node_modules/@angular/core/fesm5/core.js?:1598:24) at tryResolveToken (webpack:/ //./node_modules/@angular/core/fesm5/core.js?:1542:16) 在 StaticInjector.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:1439: 20) 在 resolveNgModuleDep (webpack:///./node_modules/@angular/core/fesm5/core.js?:8667:29) 在 NgModuleRef_.get (webpack:///./node_modules/@angular/core/fesm5 /core.js?:9355:16) 在 resolveDep (webpack:///./node_modules/@angular/core/fesm5/core.js?:9720:45)

请帮忙。 我已经尝试从我的模板中删除路由器链接。

TestBed.configureTestingModule({
    declarations: [
      AppComponent
    ],
    imports: [
      CoreModule.forRoot(),
      RouterTestingModule.withRoutes(routes),
    ],
    providers: [
      {provide: APP_BASE_HREF, useValue: '/'},
    ]
}

我可以知道您是否导入了RouterTestingModule吗? 你应该这样导入:

import { RouterTestingModule } from '@angular/router/testing';

此外,什么是routesRouterTestingModule.withRoutes(routes) 以下是如何将 RouterTestingModule 导入测试台的示例。

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      HttpClientTestingModule,
      RouterTestingModule.withRoutes([]),
    ],
    declarations: [
      SomeComponent,
    ],
    providers: [
      SampleService,
    ],

  })
    .compileComponents()
}))

我能够解决这个问题。 在修改测试用例时,我更新了 @types/jasmine 的版本以解决一些问题并更新了一些其他依赖项。 在后台,我的应用程序也因此而崩溃。 因此,我重新分叉了存储库,然后仅更新了所需的依赖项。 现在代码工作正常。

暂无
暂无

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

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