【问题标题】:Route with children cannot be match, when navigating to it导航到它时,无法匹配带有孩子的路线
【发布时间】:2016-07-28 17:43:25
【问题描述】:

美好的一天。我不明白 Angular 2 rc 4 中带孩子的新路线是如何工作的。 我想路由到有孩子的 TestComponent,但出现错误“cannot match any route '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 的构造函数中。

【问题讨论】:

    标签: typescript angular


    【解决方案1】:

    有几件事

    • pathMatch: 'full',添加到第一条路由,否则例如/ping将在LogInComponent中搜索子路由ping

    • 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
            }
          ]
        }  
    ];
    

    【讨论】:

    • 成功了!谢谢。有什么办法不使用默认路由?
    • 据我所知不知道。
    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2011-06-14
    相关资源
    最近更新 更多