Angular 2 router.navigate
If the first segment doesn't start with /
it is a relative route. router.navigate
needs a relativeTo
parameter for relative navigation
Either you make the route absolute:
this.router.navigate(['/foo-content', 'bar-contents', 'baz-content', 'page'], this.params.queryParams)
or you pass relativeTo
this.router.navigate(['../foo-content', 'bar-contents', 'baz-content', 'page'], {queryParams: this.params.queryParams, relativeTo: this.currentActivatedRoute})
See also
import { ActivatedRoute } from '@angular/router';export class ClassName { private router = ActivatedRoute; constructor(r: ActivatedRoute) { this.router =r; }onSuccess() { this.router.navigate(['/user_invitation'], {queryParams: {email: loginEmail, code: userCode}});}}Get this values:---------------ngOnInit() { this.route .queryParams .subscribe(params => { let code = params['code']; let userEmail = params['email']; });}
Ref: https://angular.io/docs/ts/latest/api/router/index/NavigationExtras-interface.html
As simpler as
import { Router } from '@angular/router';
constructor( private router:Router) {}
return(){this.router.navigate(['/','input']);}
Here you will be redirecting to route input .If you wish to go to particular path with relative to some path then.
return(){this.router.navigate(['/relative','input']);}
Here on return() is the method we will be triggered on a button click
<button (click)=return()>Home