get router params angular code example
Example 1: angular router with wuery param
goProducts() {
this.router.navigate(['/products'], { queryParams: { order: 'popular' } });
}
http://localhost:4200/products?order=popular
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.queryParams
.filter(params => params.order)
.subscribe(params => {
console.log(params);
this.order = params.order;
console.log(this.order);
}
);
}
Example 2: get url params angular
import { ActivatedRoute, Params } from '@angular/router';
@Component(...)
export class MyComponent implements OnInit {
constructor (private activatedRoute: ActivatedRoute){}
ngOnInit() {
this.activatedRoute.params.subscribe((params: Params) => {
if (params.myParam){
}
});
}
}
Example 3: angular navigate using component
import {Router} from '@angular/router';
export class Component{
constructor(private route:Router){}
go(){
this.route.navigate(['/page']);
}
}
Example 4: angular routing url params
const appRoutes: Routes = [
{ path: 'crisis-center/:param1', component: CrisisListComponent },
{ path: 'hero/:param2', component: HeroDetailComponent },
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{ enableTracing: true }
)
],
...
})
export class AppModule { }
Example 5: router params angular
this.router.navigate(['/users/edit/', user.id]);