1<a class="nav-link" [routerLink]="['/home']" (click)="doSomeLogic()">Home</a>
2
1//ng new routing-app --routing
2//ng generate component first
3//ng generate component second
4
5import { Router } from '@angular/router';
6
7...
8
9export class AboutUserComponent implements OnInit {
10 user: User;
11
12 constructor(
13 private route: ActivatedRoute,
14 private service: UserService,
15 private router: Router
16 ) {}
17
18 ngOnInit() {
19 // grab the current username
20 let username = this.route.snapshot.params['username'];
21 this.service.getUser(username).then(user => this.user = user);
22 }
23
24 goBack() {
25 this.router.navigate(['/about']);
26 }
27
28}
1import { Router } from '@angular/router';
2
3constructor(
4 private router:Router
5 ) { }
6
7redirectFunction(){
8 this.router.navigate(['hello/redirect/pageURL'],{queryParams:{id:1234,name:"ash"}});
9}
1import {Router} from '@angular/router'; // import router from angular router
2
3export class Component{ // Example component..
4 constructor(private route:Router){}
5
6 go(){
7 this.route.navigate(['/page']); // navigate to other page
8 }
9}
1<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">
2 link to user component
3</a>
1// Here’s a basic example using the navigate method:
2
3goPlaces() {
4 this.router.navigate(['/', 'page-name']);
5}
6
7/*
8I hope it will help you.
9Namaste
10*/