简体   繁体   English

无法匹配任何路线。 角度误差?

[英]Cannot match any routes. Error in angular?

I made a simple app which is running correctly. 我做了一个简单的app ,它可以正常运行。

Now I am trying to write test cases of that application, so I tried with routing . 现在,我正在尝试编写该应用程序的测试用例,因此我尝试使用routing

stackblitz 突击

My routing code is this 我的路由代码是这样

Main module: 主模块:

export const routes: Routes = [
  { path: '',   redirectTo: '/users', pathMatch: 'full' },

];
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    UserModule,
    HttpClientModule,
    RouterModule.forRoot(routes)

  ],

Feature module: 功能模块:

const routes: Routes = [
  {path: 'users', component: ListingComponent}

];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(routes)
  ],
  declarations: [ListingComponent]
})

Code

I try to run my spec but I am getting above error 我尝试运行我的spec但出现错误

describe('Initial navigation', () => {
    it('default route redirects to home (async)', fakeAsync(() => {
      router.initialNavigation(); // triggers default
      fixture.detectChanges();
      tick();
      console.log('==================');
      console.log(location.path());
      // fixture.whenStable().then(() => {
      expect(location.path()).toBe('/users');
      // })
    }));
  });

If you import UserModule to the spec, this resolves the error. 如果将UserModule导入规范,则可以解决该错误。 As AppModule modules imports UserModule to register user feature/module routes, it must also be imported in the spec to ensure it's route registrations are available in the spec as well. AppModule模块导入UserModule以注册用户功能/模块路线时,还必须将其导入规范中,以确保其路线注册在规范中也可用。

The need for this is implied at a basic level in the documentation Testing: Import a feature module . 在文档“ 测试:导入功能模块”中基本隐含了对此的需求。

//setup
beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [
      UserModule, // import module
      RouterTestingModule.withRoutes(routes)],
    declarations: [
      TestComponent,
    ]
  });
  fixture = TestBed.createComponent(TestComponent);
  router = TestBed.get(Router);
  location = TestBed.get(Location);
});

Here is an updated StackBlitz demonstrating the functionality (test passing with no error). 这是演示功能的更新StackBlitz (测试通过,无错误)。

暂无
暂无

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

相关问题 无法匹配任何路由。 带参数的 Angular2 URL - Cannot match any routes. Angular2 URL with parameters 角单元测试:未捕获错误:未捕获(承诺中):错误:无法匹配任何路线。 网址段:“注销” - Angular Unit Test: Uncaught Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'logout' Angular 测试:未捕获错误:未捕获(承诺中):错误:无法匹配任何路由。 URL 段:“家” - Angular Testing: Uncaught Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home' 角度故事书错误:无法匹配任何路线。网址细分:'iframe.html' - Angular storybook Error: Cannot match any routes. URL Segment: 'iframe.html' Angular 单元测试:错误:无法匹配任何路由。 URL 段:'home/advisor' - Angular Unit testing : Error: Cannot match any routes. URL Segment: 'home/advisor' Angular 如何解决错误:无法匹配任何路由。 URL 段:“目录/测试”? - Angular how do I resolve Error: Cannot match any routes. URL Segment: 'catalog/test'? 错误:无法匹配任何路由。 URL 段:'?' - Error: Cannot match any routes. URL Segment: '?' 错误:无法匹配任何路线。 网址段:“帐户” - Error: Cannot match any routes. URL Segment: 'account' ERROR 错误:未捕获(在承诺中):错误:无法匹配任何路由。 URL 段:'2017-18' - ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: '2017-18' 错误错误:未捕获(承诺),无法匹配任何路由。 URL 段 - ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM