Redirect within component Angular 2
Asked Answered
P

4

82

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?

Plaque answered 1/10, 2015 at 20:35 Comment(1)
I can only think of routing. You can do that with navigateChoose
M
103

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>']);
Markowitz answered 2/10, 2015 at 9:35 Comment(3)
FYI: navigateByUrl doesn't accept an array. Just a single string.Improbability
How do I reload the view? navigate and navigateByUrl seem to change change the url but the components bound to the route are not reloaded.Aelber
Note: This answer addresses a beta or release-candidate version of Angular 2 and is no longer applicable for Angular 2 final.Demarcate
D
8

@kit's answer is okay, but remember to add ROUTER_PROVIDERS to providers in the component. Then you can redirect to another page within ngOnInit method:

import {Component, OnInit} from 'angular2/core';
import {Router, ROUTER_PROVIDERS} from 'angular2/router'

@Component({
    selector: 'loginForm',
    templateUrl: 'login.html',
    providers: [ROUTER_PROVIDERS]
})

export class LoginComponent implements OnInit {

    constructor(private router: Router) { }

    ngOnInit() {
        this.router.navigate(['./SomewhereElse']);
    }

}
Devalue answered 8/4, 2016 at 8:20 Comment(2)
Note: This answer addresses a beta or release-candidate version of Angular 2 and is no longer applicable for Angular 2 final.Demarcate
@Demarcate could you please then provide an updated answer please?Uncurl
C
8

This worked for me Angular cli 6.x:

import {Router} from '@angular/router';

constructor(private artistService: ArtistService, private router: Router) { }

  selectRow(id: number): void{
       this.router.navigate([`./artist-detail/${id}`]);

  }
Conservatism answered 9/7, 2018 at 16:5 Comment(0)
S
1
callLog(){
    this.http.get('http://localhost:3000/getstudent/'+this.login.email+'/'+this.login.password)
    .subscribe(data => {
        this.getstud=data as string[];
        if(this.getstud.length!==0) {
            console.log(data)
            this.route.navigate(['home']);// used for routing after importing Router    
        }
    });
}
Serenaserenade answered 30/1, 2020 at 12:30 Comment(1)
Hi PYRO BUG, welcome. Please consider adding an explanation.Dunant

© 2022 - 2024 — McMap. All rights reserved.