Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/07/2017, 14:19
Avatar de lekuack
lekuack
 
Fecha de Ingreso: agosto-2012
Ubicación: Cabrero
Mensajes: 35
Antigüedad: 11 años, 6 meses
Puntos: 0
Exclamación Problema al redireccionar con Angular

Hola estoy realizando un login con angular el login y todo me funciona bien, pero al intentar redireccionar no funciona y me da un error en la consola del navegador

Cita:
TypeError: Cannot read property 'navigate' of undefined
at Object.next (login.component.ts:26)
mi componete login es el siguiente
Código:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import {Observable} from 'rxjs/Rx';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  loginForm: FormGroup;
  user: Observable<firebase.User>;
  returnUrl: string;

  constructor(public afAuth: AngularFireAuth, private router: Router) {
    this.user = afAuth.authState;
  }

  ngOnInit() {
    this.afAuth.auth.onAuthStateChanged(function(user){
      if (user) {
        this.router.navigate(['/home']);
      }
    });
    this.loginForm = new FormGroup({
      'email': new FormControl('', Validators.required),
      'password': new FormControl('', Validators.required)
    })
  }

  login(){
    this.afAuth.auth.signInWithEmailAndPassword(this.loginForm.get('email').value, this.loginForm.get('password').value).then(function(user){
      this.router.navigate(['/home']);
    }, function(error) {
      console.log("Error Login: ", error);
    });
  }
}
mi routing es el siguiente:

Código:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  { path: '', redirectTo: 'login', pathMatch: 'full' },
  { path: 'login', component: LoginComponent },
  { path: 'home', component: HomeComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: []
})
export class L3RoutingModule { }