-2

I have never done this before so that is why i have put this question here and need an explanation or an example blog that can tell me exactly how i can create a dynamic url like this demo.com/username. Currently my routes are working like this demo.com/settings/emailsettings but i need to implement the logic that it becomes demo.com/username/settings/emailsettings.

Note: The above urls are just for example and they are not real.

Ehsan Nissar
  • 643
  • 2
  • 13
  • 35

2 Answers2

2

in your routes you need to define a param

{
  path: '/:username/settings/emailsettings',
  component: YourComponent
}

Then, when navigate by code you will pass the username to the route dynamically.

From component class:

this.navigate([user.username, 'settings', 'emailsettings'];

From component template

[routerLink]="[user.username, 'settings', 'emailsettings']"
jalex19
  • 201
  • 2
  • 7
1

Please try below code:

let url='demo.com/settings/emailsettings';
let pos=url.indexOf('/')+1;
let userName='myUserName/';
let outputUrl=[this.url.slice(0,pos),userName,this.url.slice(pos)].join('');
Piyali Das
  • 80
  • 5