angular - 为 Angular 2 中的每个用户自定义 URL

标签 angular url url-rewriting customization

我想看到用户的用户名作为 URL 的一部分。例如:

我有一个这样的 URL:

 www.undefiend.com/panel/dashboard

我想为每个用户定制它。像这样:

www.undefiend.com/user1/dashboard
  • www.undefiend.com/michael/mails

  • www.undefiend.com/richard/profile

其他...

我如何在 Angular 2 或更高版本中做到这一点?

任何帮助都会很棒。感谢您的帮助。

最佳答案

在路由中使用带参数的路由器

routes = [
  { path: ':user', children: [
      { path: 'dashboard', component: DashboardComponent, }
  ]},
  { path: '**', component: 'LoginComponent' }
];

然后在登录组件中,您可以导航到用户所在的路线

this.router.navigate(['/' + username, 'dashboard']);

另见 https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard

关于angular - 为 Angular 2 中的每个用户自定义 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42457868/

相关文章:

php - 在在线主机中将 NGINX 重写为 Laravel 5

http - Angular2 - 如何在组件中链接异步服务调用(http 请求)?

python - 如何临时将 Django 站点移动到子目录中(用于测试)?

codeigniter - 如何在Windows Azure上重写Codeigniter的index.php

node.js - 是否可以为多个本地主机项目设置自定义开发域?

javascript - 在 Vue js 中单击链接后如何添加 URL 参数?

asp.net-mvc - URL 重写和 IIS Express : some rules work, 有些没有

angular - 在运行时以 Angular 关闭动画

angular - 有没有办法使用 FormArray 作为顶级表单

apache - Apache 上的 Angular 2 |搜索引擎爬虫