【问题标题】:Angular route gets used as parameter from default route角度路由被用作默认路由的参数
【发布时间】:2019-09-20 16:09:00
【问题描述】:

我有一个默认路由 '' 去我的应用程序的 HomeComponent。我还有另一条路线,您可以使用/other 导航到该路线。现在您可以使用参数:param 从应用程序中的任何位置返回 HomeComponent,发生的情况是:/other 路由被用作参数,因此它不会重定向我,但作为:param 我得到other在 HomeComponent 中。

   const routes: Routes = [
        {path: '', component: HomeComponent},
        {path: ':param', component: HomeComponent},
        {path: 'other', component: OtherComponent},
   ];

也尝试过作为子路由

const routes: Routes = [
            {path: '', component: HomeComponent,
             children: [
               {path: 'other', component: OtherComponent}
             ],
            },
            {path: ':param', component: HomeComponent},
       ];

有什么解决方案可以让我在默认路由和子路由/其他路由上有参数吗?

【问题讨论】:

    标签: angular routing


    【解决方案1】:

    你可以试试这个方法:

     const routes: Routes = [
        {path: '**', component: HomeComponent},
        {path: '', component: HomeComponent},
        {path: ':param', component: HomeComponent},
        {path: 'other', component: OtherComponent},
    

    ];

    【讨论】:

      【解决方案2】:

      看来路线的顺序很重要

      const routes: Routes = [
          {path: '', component: HomeComponent},
          {path: 'other', component: OtherComponent},
          {path: ':param', component: HomeComponent},
      

      ];

      通过将路线移动到参数上方,它仍然导航到路线。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-22
        • 1970-01-01
        • 1970-01-01
        • 2016-08-21
        • 2018-05-17
        • 2016-03-12
        • 1970-01-01
        相关资源
        最近更新 更多