首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于字符串第一个字符的Angular 9路由

基于字符串第一个字符的Angular 9路由
EN

Stack Overflow用户
提问于 2020-11-03 01:07:02
回答 1查看 64关注 0票数 1

在我的Angular 9应用程序中,当路径以"@“开头时,我想导航到某个页面。

例如,"mywebsite.com/@sample- User“应该导航到用户组件,"mywebsite.com/@sample-user/:post-id”导航到UserPostView组件,"mywebsite.com/sample-user“(路径开头没有@)应该导航到不同的组件。

代码语言:javascript
复制
const routes: Routes = [{path: '@:username', component: UserComponent}, {path: '@:username/:pist-id', component: UserPostViewComponent}, {path: 'sample-user', component: InternalComponent}]

我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2020-11-03 01:45:51

我认为你可以使用一个自定义的匹配器和正则表达式来实现这一点。

代码语言:javascript
复制
RouterModule.forRoot([
 {
    matcher: (url) => {
      if (url.length === 1 && url[0].path.match(/^@[\w]+$/gm)) {
        return {
          consumed: url,
          posParams: {
            username: new UrlSegment(url[0].path.substr(1), {})
          }
        };
      }
      return null;
    },
    component: ProfileComponent
 }
])

看看这篇文章,它有所有的答案。

https://medium.com/@brandontroberts/custom-route-matching-with-the-angular-router-fbdd48665483

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64650053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档