【问题标题】:Angular: (Forwarding to) Routing with multiple variablesAngular:(转发到)具有多个变量的路由
【发布时间】:2021-02-04 19:47:07
【问题描述】:

我什至不确定这是否可行,因为我以前从未见过或做过。我在文档中找不到任何类似的东西。但是,基本上我想要实现的是这样的:

route1: /blog/author/1 ==> Redirects to route 2
route2: /blog/author/1/Jane_Doe ==> Redirects to route 3
route3: /blog/author/1/Jane_Doe/...

如果我可以将非 ID 变量的 / 替换为 -,那就更好了。因此:

route1: /blog/author/1 ==> Redirects to route 2
route2: /blog/author/1/Jane_Doe ==> Redirects to route 3
route3: /blog/author/1/Jane_Doe-...

我尝试了以下方法:

const routes: Routes = [
  {
    path: ":id",
    redirectTo: ":id/:fullname"
  },
  {
    canActivateChild: [MetaGuard],
    path: ":id/:fullname",
    component: DetailedAuthorItemComponent,
    resolve: {
      author: AuthorResolver,
      posts: AuthorPostsResolver,
    },
  },
];

但是,第一个路径不起作用。我收到以下错误:

ERROR Error: Uncaught (in promise): Error: Cannot redirect to ':id/:fullname'. Cannot find ':fullname'.

任何可以帮助我并知道这是否可能的人?

【问题讨论】:

    标签: angular routes angular-routing


    【解决方案1】:

    您可以使用角度路由中的 routes 属性的子项来实现此目的。像这样的:

    这被称为角度路由中的嵌套子概念

    const routes: Routes = [
    {
      path: 'blog',
      component: Component1, // The route here would be /blog
      children: [
         {
            path: 'author',
            component: Component2, // Basically the path here would be /blog/author
            children: [
               {
                  path: 'id',
                  component: Component3, // Basically the path here would be /blog/author/id
               }
            ]
         }
      ]
    }
    ]
    

    【讨论】:

    • 我认为你误解了我想要达到的目标。我知道子路由,并且我已经在我的不同路由模块中通过延迟加载实现了它,但我希望实现将不同的路径转发到同一个 URL,同时在路由中也有多个变量。
    猜你喜欢
    • 2018-05-12
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2019-01-08
    • 2014-04-18
    • 2020-10-02
    • 2021-12-01
    相关资源
    最近更新 更多