【问题标题】:Angular 6 - Dynamic Routing with PrefixAngular 6 - 带前缀的动态路由
【发布时间】:2018-06-02 07:13:20
【问题描述】:

我正在开发一个 Angular 通用应用程序。我想创建带有自定义前缀的动态路由,但我找不到任何与我的案例相关的有用文档。任何帮助将不胜感激...

详情:

我有 4 个页面,其中有 4 个不同的动态 URL,它们是:

  • 主页 (http://example.com/)
  • 分类页面 (http://example.com/{category_name})
  • 子类别页面 (http://example.com/{category_name}/{sub_category_name})
  • 产品页面 (http://example.com/p{product_id}-{product_name})
  • 用户页面 (http://example.com/user{user_id}-{user_name})

我做了什么

我已经注册了一个路由来处理主页、类别和子类别页面,因为它们具有相同的 UI 以及下面提到的动态类别级别,

RouterModule.forRoot([
      {path: '**', component: HomeComponent, data: {title: 'Home', description: 'Homepage - quick overview.'}}
    ])

苦苦挣扎:

现在,我无法为产品和用户页面添加路由,我无法理解如何在产品和用户页面中分别在斜杠之后和 ids 之前添加 puser 前缀。没有这些前缀,路由就可以正常工作。

产品和用户页面所需的 URL 示例

我正在使用@angular/router 进行路由。

提前致谢。

【问题讨论】:

标签: angular angular-router angular6 dynamic-url


【解决方案1】:

谢谢@Yuriy 重新打开这个,我已经从@Ingo Bürk 的评论中得到了答案。 下面提到的 Gist 帮助我通过正则表达式创建路由。 https://gist.github.com/matanshukry/22fae5dba9c307baf0f364a9c9f7c115

作为参考,我在下面添加了来源,

/**
 * Copyright (c) Matan Shukry
 * All rights reserved.
 */

import { UrlSegment, UrlSegmentGroup, Route } from '@angular/router';

// export type UrlMatchResult = {
    // consumed: UrlSegment[]; posParams?: { [name: string]: UrlSegment };
// };

export function ComplexUrlMatcher(paramName: string, regex: RegExp) {
    return (
        segments: UrlSegment[],
        segmentGroup: UrlSegmentGroup,
        route: Route) => {

        const parts = [regex];
        const posParams: { [key: string]: UrlSegment } = {};
        const consumed: UrlSegment[] = [];

        let currentIndex = 0;

        for (let i = 0; i < parts.length; ++i) {
            if (currentIndex >= segments.length) {
                return null;
            }
            const current = segments[currentIndex];

            const part = parts[i];
            if (!part.test(current.path)) {
                return null;
            }

            posParams[paramName] = current;
            consumed.push(current);
            currentIndex++;
        }

        if (route.pathMatch === 'full' &&
            (segmentGroup.hasChildren() || currentIndex < segments.length)) {
            return null;
        }

        return { consumed, posParams };
    }
}

如何使用,

/**
 * Copyright (c) Matan Shukry
 * All rights reserved.
 */

export const UserRoutes: Routes = [
  {
    path: 'users',
    component: UserComponent,
    children: [
      {
        path: '',
        component: UserListComponent
      },
      {
        matcher: ComplexUrlMatcher("id", /[0-9]+/),
        component: UserItemComponent
      },
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(UserRoutes)],
  exports: [RouterModule]
})
export class UserRoutingModule { }

【讨论】:

    【解决方案2】:

    通常,router 需要能够将某个输入字符串 (url) 与给定模式 (route) 匹配。您要确保 single 输入不匹配 multiple 模式,否则路由器将不知道前进的方向。

    话虽如此,Angular 具有[RouteGuard][1] 的概念。当 url 与给定路由匹配时,RouteGuard(或其任何派生类)会在路由过程中提供挂钩。

    【讨论】:

      【解决方案3】:

      你可以使用“匹配器”。

      见:https://angular.io/api/router/UrlMatcher

      我希望它有所帮助。 享受吧!

      【讨论】:

        【解决方案4】:

        要创建http://example.com/p{product_id}-{product_name},我们可以将product_id 段拆分为:product_id,将product_name 段拆分为:product_name。然后你需要前往你的 app.routing.js 路由文件并将 url 设置为:

        { path: 'http://example.com/p/:product_id/-/:product_name', component: ProductComponent } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-26
          • 1970-01-01
          • 2017-06-07
          • 2015-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多