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

时间:2017-05-02 15:29:43

标签: angular firebase rxjs combinelatest

我已经使用Angular和Firebase构建了一些应用程序,而我只是开始了一个新的应用程序。我对其他人如何处理使用observable获取数据感兴趣。在我的特定应用程序中,我需要使用observable从URL获取参数,检查用户是否经过身份验证,并从firebase获取2个不同的对象。我需要首先检查auth,然后从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,但看起来我不能使用从一个observable返回的数据在另一个中使用。我的问题是,有没有人在没有嵌套订阅的情况下解决了类似的问题?

以下是我的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"
  ]
}

1 个答案:

答案 0 :(得分:1)

我就是这样做的

后卫:

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}`);
    }

  }