Reputation: 17
so basically i want tho get something from my server using the id of the object.
The id is also in the url:
http://xyz/detail/5ee8cb8398e9a44d0df65455
So now im trying the following in xyz.component.ts:
getEx(): void {
let id:string = +this.route.snapshot.paramMap.get('id');
const x = this.exService.getEx(id)
.subscribe(ex => this.ex = ex);
}
But this does not work because paramMap.get returns a number and not a string.
Type 'number' is not assignable to type 'string'.ts(2322)
The last part of my url is obv a string, how can i get this string?
Thanks for your help!
Upvotes: 0
Views: 7122
Reputation: 1627
Remove the +
sign from +this.route
in your code. In this way it is trying to convert the id
which is string to int
.
That's why it's throwing Type 'number' is not assignable to type 'string'.ts(2322)
error
getEx(): void {
let id:string = this.route.snapshot.paramMap.get('id');
const x = this.exService.getEx(id)
.subscribe(ex => this.ex = ex);
}
Upvotes: 0
Reputation: 47
id: any;
constructor(public route: ActivatedRoute) { }
ngOnInit(): void {
this.route.queryParams.subscribe(params => {
this.id = params['id'];
})
}
It can read id from the link format: localhost:4200/componentname?id=abcdef
and will return value abcdef in the id variable
Upvotes: 1
Reputation: 6286
route.snapshot.paramMap.get
returns string only. You seem to have added the +
before that which is converting that to int.
It should simply be:
let id:string = this.route.snapshot.paramMap.get('id');
Upvotes: 3