这个问题通常是在单元测试中发生的,因为在单元测试中使用的路由器是特定的路由器,而不是应用程序中的路由器。因此,需要在测试环境中提供一个路由器配置。
以下是示例代码:
在你的测试文件中,使用TestBed来配置路由器并使用TestingModule来导入所需的模块和组件:
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule // import the RouterTestingModule here
],
declarations: [
AppComponent
],
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
});
这个示例中,RouterTestingModule被导入以供使用。这允许在测试环境中使用路由器。 它应该被导入到任何需要在单元测试中使用路由器的测试文件中。