Foros del Web » Programando para Internet » Node.js »

Problema al redireccionar con Angular

Estas en el tema de Problema al redireccionar con Angular en el foro de Node.js en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 25/07/2017, 14:19
Avatar de lekuack  
Fecha de Ingreso: agosto-2012
Ubicación: Cabrero
Mensajes: 35
Antigüedad: 11 años, 7 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 { }

Etiquetas: angular, redireccionar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:59.