导航到儿童的路线无法匹配

时间:2016-07-28 09:31:35

标签: typescript angular

美好的一天。我无法理解Angular 2 rc 4中有孩子的新路线是如何工作的。 我想路由到 TestComponent ,它有子级,但错误" 无法匹配任何路由' test' "出现。我这样做路由:this.guide.navigate(['test']);。 这是我申报路线的方式:

const routes: RouterConfig = [
    {
        path: '',
        component: LogInComponent
    },
    {
        path: 'pin',      
        component: PinComponent,    },
    {
       path: 'test',
       component: TestComponent,
       children: [
        {
            path: '/:page',
            component: FoodComponent
        }
      ]
    }  
]

export const mainRouterProviders = [
  provideRouter(routes)
];

默认情况下,我无法在测试中使用某些页面(例如"路径:' /'")因为它的子项就像制表符一样,它们的列表加载在TestComponent' s中构造

1 个答案:

答案 0 :(得分:2)

有一些事情

  • pathMatch: 'full',添加到第一条路线,否则例如/ping会在ping

  • 中搜索子路线LogInComponent
  • 添加一个默认路由到test,重定向到某个默认ID或某个虚拟组件。 Angular不喜欢组件有<router-outlet>但没有添加组件的路径。

const routes: RouterConfig = [
    {
        path: '',
        pathMatch: 'full',
        component: LogInComponent
    },
    {
        path: 'pin',      
        component: PinComponent,    },
    {
       path: 'test',
       component: TestComponent,
       children: [
        {
            path: '',
            pathMatch: 'full',
            redirectTo: 'myDefaultId'
            // or 
            // component: DummyComponent
        },
        {
            path: '/:page',
            component: FoodComponent
        }
      ]
    }  
];
相关问题