在构建使用 Firebase 的 Angular 应用程序时如何处理所有 Observable

How to deal with all the Observables when building an Angular App that uses Firebase

我已经使用 Angular 和 Firebase 构建了一些应用程序,而我刚刚开始一个新应用程序。我对其他人如何处理使用可观察对象获取数据很感兴趣。在我的特定应用程序中,我需要使用可观察对象从 URL 获取参数,检查用户是否已通过身份验证,并从 firebase 获取 2 个不同的对象。我需要先检查身份验证,然后从 URL 获取参数,这样我才能找到 firebase 对象的位置。在过去,我只是像这样做了一堆可观察的订阅。

ngOnInit() {
    this.af.auth.subscribe(auth => {
      if (auth) {
        this.uid = auth.uid;
        this.af.database.list('objectives', {
          query: {
            orderByChild: 'AuthorID',
            equalTo: auth.uid
          }
        }).subscribe(objectives => {
          console.log(objectives);
          this.allObjectives = objectives;
        })
      } else {
        this.router.navigate(['login'])
      }
    })
  }

现在我必须添加参数并再次调用 firebase。我试过使用 rxjs 的 combineLatest,但看起来我无法使用从一个可观察对象返回的数据用于另一个对象。我的问题是,有没有人在没有嵌套订阅的情况下解决了类似的问题?

下面是我的package.json文件

{
  "name": "wdw-hunt",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.4.0",
    "@angular/compiler": "^2.4.0",
    "@angular/core": "^2.4.0",
    "@angular/forms": "^2.4.0",
    "@angular/http": "^2.4.0",
    "@angular/platform-browser": "^2.4.0",
    "@angular/platform-browser-dynamic": "^2.4.0",
    "@angular/router": "^3.4.0",
    "angular2-cookie": "^1.2.6",
    "angularfire2": "^2.0.0-beta.8",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "firebase": "^3.9.0",
    "hammer-timejs": "^1.1.0",
    "hammerjs": "^2.0.8",
    "ng2-bootstrap": "^1.4.2",
    "ng2-simple-page-scroll": "^2.0.0",
    "rxjs": "^5.1.0",
    "zone.js": "^0.7.6"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0-rc.2",
    "@angular/compiler-cli": "^2.4.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "firebase-cli": "^1.2.0",
    "firebase-tools": "^3.5.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "^2.3.2"
  }
}

下面是我的tsconfig

{
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "lib": [
      "es2016",
      "dom"
    ],
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "",
    "types": []
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

我就是这样做的

后卫:

import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {AngularFire} from 'angularfire2';
import {isNullOrUndefined} from 'util';

@Injectable()
export class OverviewGuard implements CanActivate {
  constructor(private af: AngularFire, private router: Router) {

  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    return this.af.auth.map(auth => {
      if (isNullOrUndefined(auth)) {
        this.router.navigate(['/login']);
        return false;
      } else {
        return true;
      }
    });
  }
}

一些供应商:

import {Injectable} from '@angular/core';
import {AngularFire} from 'angularfire2';
import {Observable} from 'rxjs/Observable';

  @Injectable()
  export class JobProvider {
    constructor(private af: AngularFire) {
    }

    public getSomething(): Observable<Something> {
      return this.getUserDb()
        .switchMap(path => this.af.database.object(`${path}/something`));
    }

    private getUserDb(): Observable<string> {
      return this.af.auth
        .map(auth => `/debug/users/${auth.uid}`);
    }

  }