angular router params 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); // { order: "popular" }
this.order = params.order;
console.log(this.order); // popular
}
);
}
Example 2: angular navigate using component
import {Router} from '@angular/router'; // import router from angular router
export class Component{ // Example component..
constructor(private route:Router){}
go(){
this.route.navigate(['/page']); // navigate to other page
}
}
Example 3: angular router navigate
// Here’s a basic example using the navigate method:
goPlaces() {
this.router.navigate(['/', 'page-name']);
}
/*
I hope it will help you.
Namaste
*/
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 } // <-- debugging purposes only
)
// other imports here
],
...
})
export class AppModule { }
Example 5: router params angular
// Navigate and send Params
this.router.navigate(['/users/edit/', user.id]);
Example 6: how to add changes every time you route navigate to page in angular
// Place import at the top
import { Router,NavigationStart} from '@angular/router';
//Place the code below inside your class
constructor(private router: Router){}
ngOnInit(){
this.router.events.subscribe(event =>{
if (event instanceof NavigationStart){
}
})
}