gpt4 book ai didi

angular - 错误 : StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: (NullInjectorError: No provider for Router!)

转载 作者:太空狗 更新时间:2023-10-29 18:22:10 32 4
gpt4 key购买 nike

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

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

error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'RouterLinkWithHref', Function ], ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, 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, outputs: [ ], 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: null }), Object({ ... Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: StaticInjectorError(Platform: core)[RouterLinkWithHref -> Router]: NullInjectorError: No provider for Router! at NullInjector.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:1360:19) at 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) at StaticInjector.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:1439:20) at resolveNgModuleDep (webpack:///./node_modules/@angular/core/fesm5/core.js?:8667:29) at NgModuleRef_.get (webpack:///./node_modules/@angular/core/fesm5/core.js?:9355:16) at 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?你应该这样导入它:

从'@angular/router/testing'导入{RouterTestingModule};

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

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

})
.compileComponents()
}))

关于angular - 错误 : StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: (NullInjectorError: No provider for Router!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415443/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com