How to create simple redirect on click on some button in Angular 2? Already tried:
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']);
    }
}
                You could leverage the event support of Angular2:
import {Router} from '@angular/router';
@Component({
  selector: 'loginForm',
  template: `
    <div (click)="redirect()">Redirect</div>
  `,
  providers: [ROUTER_PROVIDERS]
})
export class LoginComponent implements OnInit {
  constructor(private router: Router) { }
  redirect() {
    this.router.navigate(['./SomewhereElse']);
  }
}
                        I would make it more dynamic using method parameters
Import the angular router
import { Router } from '@angular/router';
Create a button with click event
<div (click)="redirect(my-page)">My page</div>
Create a method with a pagename parameter
redirect(pagename: string) {
  this.router.navigate(['/'+pagename]);
}
When clicked the router should route to the correct page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With