简体   繁体   English

路由和URL处理Angular

[英]Routing and URL handling Angular

as it always is, after registration backend send me a url to confirm account. 和往常一样,注册后端后,请给我发送一个网址以确认帐户。 I got full url: https://app.com/confirm/ {userName}/{token} 我有完整的网址: https : //app.com/confirm/ {userName} / {token}

Username and token are data that I need to handle and send in back request. 用户名和令牌是我需要处理并发送回请求的数据。 The thing is I don't know how to handle it. 问题是我不知道该如何处理。 When I make routing: 当我进行路由时:

/confirm/:userName/:token

It's wrong because token got unknown number of / (slashes) so routing think that this is the path for it. 这是错误的,因为令牌具有未知的/(斜杠)数,因此路由认为这是它的路径。 How can I resolve that problem? 我该如何解决这个问题? I tried with RouterActivated and RouterActivatedSnapshot. 我尝试使用RouterActivated和RouterActivatedSnapshot。 I got ** with redirect to error page so when I do it as above, it always go to error page. 我有重定向到错误页面的**,所以当我如上所述进行操作时,它总是转到错误页面。 Any suggestions? 有什么建议么?

In my opinion you should: 我认为您应该:

  1. Encode url before adding it to email 在将网址添加到电子邮件之前对其进行编码
  2. Pass token as query param - because token may be very long 将令牌作为查询参数传递-因为令牌可能很长

You can try to use custom UrlMatcher : 您可以尝试使用自定义UrlMatcher

...
const appRoutes: Routes = [
  {
    matcher: tokenMatcher,
    component: TestComponent
  }
];
...

/**
 * custom url matcher for router config
 */
export function tokenMatcher(url: UrlSegment[]) {

  if (url.length > 2 && url[0].path === 'confirm') {
    return {
      consumed: url,
      posParams: {
        userName: url[1],
        token: new UrlSegment(url.slice(2).map(u => u.path).join('/'), {})
      }
    }
  }

  return null;
}

STACKBLITZ STACKBLITZ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM