如何在规格中导入路线

时间:2018-12-16 20:52:55

标签: angular6 angular-routing

我在app-routes.ts中将以下路由定义为单独的模块

import {NgModule} from '@angular/core';
import {RouterModule, Routes} from "@angular/router";
import {AppComponent} from "./app.component";
...

const routes:Routes = [
  {
    path: 'new',
    component: New
    canActivate:[AuthGuard] 
  },
  {
    path:'list',
    component:List
  },
  {
    path: 'signup',
    component: SignupComponentComponent 
  },
  {
    path: '', 
    redirectTo: 'home',
    pathMatch:'full'
  },
  {
    path: 'home',
    component:HomepageContentComponentComponent 
  },
  { path: '**',
    component: PageNotFoundComponent }
];


@NgModule({
  imports:[RouterModule.forRoot(routes)], //
  exports: [RouterModule],
  providers:[]
})

export class AppRoutingModule{}

我正在使用HomePageComponent中的路由。如何测试路由?我知道我需要像下面这样使用RouterTestingModule.

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports:[RouterTestingModule.withRoutes(routes)], //routes is undefined here
      declarations: [ HomepageContentComponentComponent,
      ]
    })
    .compileComponents();
  }));

但是在routes中定义为const routes:Routes = ...的{​​{1}}变量在这里不可用。

问题1)如何在当前设置下使AppRoutingModuleroutes上可见

问题2)我想创建一个单独的文件specs,该文件仅包含myroutes.ts数组,以便我可以在规范中使用它,但即使这样也不起作用。为什么? (如下所示)

routes

为什么我在单独的文件中创建my-route.ts const routes:Routes = [ {... ]; 时可以使用routes中的imports:[RouterTestingModule.withRoutes(routes)]

1 个答案:

答案 0 :(得分:0)

我应该将routes导出为export const routes:Routes