Firebase Angular 2 - 注销时无法读取null的属性'auth'

时间:2017-02-07 08:07:27

标签: javascript angular firebase firebase-authentication angularfire2

尝试使用angularfire2的logout()函数时得到<small *ngIf="!firstname.valid">Not valid!</small>

firebase.service.ts

Cannot read property 'auth' of null

app.component.ts

import { Injectable } from '@angular/core';
import { AngularFire, FirebaseListObservable, AuthProviders, AuthMethods } from 'angularfire2';
import * as firebase from 'firebase';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';

@Injectable()
export class FirebaseService {

  constructor(public af: AngularFire) { }

  fbLogin() {
    return this.af.auth.login({
      provider: AuthProviders.Facebook,
      method: AuthMethods.Popup
    });
  }

  mailLogin(data) {
    console.log(data.email);
    return this.af.auth.login({
      email: data.email,
      password: data.password
    }, {
      provider: AuthProviders.Password,
      method: AuthMethods.Password
    });
  }

  **signOut() {
    return this.af.auth.logout();
  }**

  getInfo() {
    let name: string;
    this.af.auth.subscribe(auth => auth.auth.displayName === null ? name = auth.auth.email : name = auth.auth.displayName);
    return name;
  }
}

app.component.ts的HTML

import { Component, OnInit, Input } from '@angular/core';
import { FirebaseService } from '../../services/firebase.service';
import { Router } from '@angular/router';

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

  name: string = '';
  chats: Chat[];

  constructor(public firebaseService: FirebaseService, private router: Router) { }

  ngOnInit() {
    this.name = this.firebaseService.getInfo();
  }

  **logout() {
    this.firebaseService.signOut();
  }**

依赖注入有效,因为我可以成功登录并检索当前身份验证的显示名称或电子邮件

0 个答案:

没有答案