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'; // 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}