Ebleme
Ebleme

Reputation: 307

Customize URL for each user in Angular 2

I want to see an user's username as part of URL. For example:

I have an URL like this:

 www.undefiend.com/panel/dashboard

I want to customize it for every user. Like this:

www.undefiend.com/user1/dashboard

an others...

How can I do that in Angular 2 or above ?

Any helps would be great. Thanks for help.

Upvotes: 3

Views: 2836

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657118

Use the router with a param in the route

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

And in the login component you then navigate to the route with the user

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

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

Upvotes: 7

Related Questions