0

Defining the Route in my app-routing :

{ path: 'user/:id', component: UserDetailComponent },

Defining the Navigation

<a [routerLink]="['/user', user.id]">detail</a>

In UserDetailComponent

this.id = +this.route.snapshot.params['id'];

Firstly. I get user detail, but when I refresh page (userdetail/:id) I get this message error

Error message when I refresh page user detail /user/:id

halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

0

I think that in the UserDetailComponent you need in the beginning of the NgOnInit you need to define this:

 const urlInfo = this.route.snapshot.paramMap.get('id');
 this.getDetails(urlInfo) //in this function you subscribe to the api call
devZ
  • 606
  • 1
  • 7
  • 23
0

I believe you missed the slash position... In your routing code, you have a slash separating users from the id, just change the code from this:

<a [routerLink]="['/user', user.id]">detail</a>

To this:

<a [routerLink]="['user/', user.id]">detail</a>
manjiro sano
  • 784
  • 3
  • 15
0

Looks like you are having base="/user" in your index.html or something else while you need it to be base="/" (assuming you are doing plain npm ng serve)

Antoniossss
  • 31,590
  • 6
  • 57
  • 99