How to pass and read query parameters in angular 2, for example http://localhost:3000/index.html?page=235 or http://localhost:300/?page=235 to get 235?
Asked
Active
Viewed 1,333 times
1
-
1Possible duplicate of [How get query params from url in angular2?](https://stackoverflow.com/questions/35688084/how-get-query-params-from-url-in-angular2) – F3L1X79 Jun 23 '17 at 11:44
-
I get this error: No provider for ActivatedRoute! – baraka Jun 23 '17 at 12:01
-
@baraka How did you solve the problem of "No provider for ActivatedRoute!" I am facing the same issue and not finding any relevant source to address it. – Pedantic Jan 29 '18 at 22:24
2 Answers
0
you can get param like that.
Example
userId: number;
constructor(private route: ActivatedRoute,
private router: Router) {
}
ngOnInit(): void {
this.subscription = this.route.params.subscribe(
(params: any) => {
if (params.hasOwnProperty('id')) {
this.userId = +params.id;
}
}
);
}
see for more details router

Shailesh Ladumor
- 7,052
- 5
- 42
- 55
0
Try This .
import {Component,OnInit} from '@angular/core';
import {Router, ActivatedRoute, Params} from '@angular/router';
@Component({...})
export class MyComponent implements OnInit{
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() {
// subscribe to params
this.activatedRoute.params.subscribe((params: Params) => {
let page= params['page'];
console.log(page);
});
}
}

shubhram krishna
- 1
- 1