【问题标题】:Angular2 Firebase PromisesAngular2 Firebase 承诺
【发布时间】:2018-07-31 05:58:14
【问题描述】:

我正在使用 Angular/Firebase 中的 Promises。成功登录后,我会使用登录用户的uid 调用 firebase。我用它来查找在 Firebase 中分配给他们的用户的“控制器名称”。这可以在snap.val() 中返回控制器名称,但我不知道如何让控制器名称输出在我的其他组件的其他位置可用。这里应该是我的 auth.service.ts 代码和相应的控制台输出,以显示它正在提取 snap.val()now 我如何解析它以获取 controllervalue 并使其可观察?在我的整个计划中?

auth.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Observable';
import { ProgramService } from '../views/shared/program.service';


@Injectable()
export class AuthService {
  public user: Observable<firebase.User>;
  public userDetails: firebase.User = null;
  public LOGGEDIN: boolean = null;
  private ownersRef: any;

  constructor(private _firebaseAuth: AngularFireAuth, private router: Router, private programService: ProgramService) {

    this.ownersRef = firebase.database().ref().child('owners');

    this.user = _firebaseAuth.authState;

    this.user.subscribe(
      (user) => {
        if (user) {
          this.userDetails = user;
          console.log(this.userDetails);
          this.getController(this.userDetails.uid);
          this.LOGGEDIN = true;
        } else {
          this.userDetails = null;
        }
      }
    );
  }

  getController(id) {
    return this.ownersRef.orderByChild('userId').equalTo(id).once('value').then((snap) => {
      console.log(snap.val());
      console.log('just returned getController snap.val');
      return snap.val();
      });
  }

//more code continues here but not needed...

登录后控制台来自 auth.service.ts 的日志数据

Angular is running in the development mode. Call enableProdMode() to enable 
the production mode.
Brian Beardmore
{-LG_PkzB4VV0AjdgKgnG: {…}}-LG_PkzB4VV0AjdgKgnG: 
controller: "Pellet_Pirate_1"
email: "dbrianbeardmore@gmail.com"
name: "Brian Beardmore"photo: "https://lh6.googleusercontent.com/-YpMyDa8szOw/AAAAAAAAAAI/AAAAAAAAACY/pw-_ei54cjs/photo.jpg"
userId: "IeA2gx1Jq4N3vDVovVXHSbOhlqu2"
just returned getController snap.val

我想存储controller: ''Pellet_Pirate_1'' 的值并使其可用于我的应用程序。此值从不在用户登录时更改。

我将不胜感激。

谢谢, 布赖恩

【问题讨论】:

    标签: angular firebase observable angularfire2


    【解决方案1】:

    最简单的方法是使用LocalStorage 来保存信息。在您的 Auth 服务中,您可能希望更新您的身份验证的订阅部分,以将信息保存到应用程序的本地存储中。

    this.user.subscribe(
          (user) => {
            if (user) {
              this.userDetails = user;
              console.log(this.userDetails);
              this.getController(this.userDetails.uid);
              this.LOGGEDIN = true;
              // save controller into local storage.
              localStorage.setItem('controller', this.getController(this.userDetails.uid));
            } else {
              this.userDetails = null;
            }
          }
        );
    

    那么你现在要做的就是每次在你的应用中加载一个页面时,检查本地存储,看看是否定义了控制器字段,否则将它们重定向到登录页面。

    这将是您的组件页面的示例构造函数:

    constructor(private router: Router) {
      if(!localStorage.getItem('controller')) {
         this.router.navigate(['/loginpath']);
      }
    
    }
    

    更复杂的方法是使用 BehaviorSubject 并监控对 Subject 的更改。

    【讨论】:

    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多