1

我已经用 Angular 和 Firebase 构建了一些应用程序,我刚刚开始一个新的应用程序。我对其他人如何使用 observables 获取数据感兴趣。在我的特定应用程序中,我需要使用 observable 从 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,但看起来我不能使用从一个 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"
  ]
}
4

1 回答 1

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

  }
于 2017-05-02T17:22:29.760 回答