Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  142] [ 3]  / answers: 1 / hits: 124258  / 9 Years ago, thu, october 1, 2015, 12:00:00

I have a simple method that at the end of it I want to redirect to another component:



export class AddDisplay{
display: any;

addPairTo(name: string, pairTo: string){
this.display = {};
this.display.name = name;
this.display.pairTo = pairTo;

}
}


What I wanna do is at the end of the method redirect to another component:



export class AddDisplay{
display: any;

addPairTo(name: string, pairTo: string){
this.display = {};
this.display.name = name;
this.display.pairTo = pairTo;

this.redirectTo('foo');
}
}


How do I achieve this in Angular 2?


More From » angular

 Answers
14

first configure routing



import {RouteConfig, Router, ROUTER_DIRECTIVES} from 'angular2/router';


and



@RouteConfig([
{ path: '/addDisplay', component: AddDisplay, as: 'addDisplay' },
{ path: '/<secondComponent>', component: '<secondComponentName>', as: 'secondComponentAs' },
])


then in your component import and then inject Router



import {Router} from 'angular2/router'

export class AddDisplay {
constructor(private router: Router)
}


the last thing you have to do is to call



this.router.navigateByUrl('<pathDefinedInRouteConfig>');


or



this.router.navigate(['<aliasInRouteConfig>']);

[#64869] Tuesday, September 29, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tristab

Total Points: 735
Total Questions: 106
Total Answers: 96

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
tristab questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Sun, Jan 31, 21, 00:00, 3 Years ago
Wed, Dec 2, 20, 00:00, 4 Years ago
Fri, Oct 23, 20, 00:00, 4 Years ago
;