Angular / Typescript / Firestore - 如何在 if 语句中返回可观察值

1 javascript scoping typescript angular google-cloud-firestore

我正在使用 angular 和 firestore。我正在检查 firestore 中我的路线守卫的价值。

我发现在页面刷新时,它返回为未定义。但是,如果我只是硬编码 true 或 false 的返回,它就可以工作。

我的 if 语句中的任何日志总是正确返回,但由于某种原因似乎没有更新我的全局变量。

如果我使用我的网站导航返回根目录并浏览我的网站,它可以正常工作。但是,当我刷新页面时,它返回为未定义。

这可能是一个范围界定问题吗?

路线守卫

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, CanActivate, Router } from '@angular/router';
import { AuthService } from './auth.service';

import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Subscription } from 'rxjs/Subscription';


//testing
import { Observable } from 'rxjs/Observable';
import { AngularFireAuth } from 'angularfire2/auth';

@Injectable()
export class CheckBillingService implements CanActivate {
    private _subscription: Subscription;
    private userBillingDocRef: AngularFirestoreDocument<any>;
    userBilling: Observable<any>;
    public activeAccount:Observable<boolean>

    constructor(private authService: AuthService,
                private router: Router,
                private auth: AngularFireAuth,
                private readonly afs: AngularFirestore) {}

    canActivate(): Observable<boolean> {
        this.authService.user.subscribe((user) => {
            if (user) {
                var userId = user.uid;

                this.userBillingDocRef = this.afs.doc('user_billing/${userId}');

                this.userBilling = this.userBillingDocRef.snapshotChanges();
                this.userBilling.subscribe((value) => {

                    const data = value.payload.data();

                    if (data.account_status == "Active") {
                        this.activeAccount = Observable.of(true);
                        console.log('inside if statement for active = ', this.activeAccount);
                    } else {
                        this.activeAccount = Observable.of(false);
                        console.log('inside if statement for not active = ', this.activeAccount);
                        this.router.navigate(['resubscribe']);
                        return Observable.of(false);
                    }

                    console.log('userBilling.subscribe = ', this.activeAccount);
                });

                console.log('just outside userBilling.subscribe = ', this.activeAccount);
            }
        });

        // When refreshig my page, this returns as undefined.
        // If I navigate to the correct page and work my way through the site it works fine
        // However, refresh returns undefined.

        console.log('out of auth = ', this.activeAccount);

        return this.activeAccount;
    }
}
Run Code Online (Sandbox Code Playgroud)

The*_*bee 5

根据您的代码,页面将在您的 observable 方法返回之前运行,因为它是异步运行的,而不是像这样返回完整的方法作为 observable

 canActivate(): Observable<boolean> {

    return Observable.create(observer=> {
this.authService.user.subscribe((user) => {

                if (user) {

                    var userId = user.uid;

                    this.userBillingDocRef = this.afs.doc('user_billing/${userId}');

                    this.userBilling = this.userBillingDocRef.snapshotChanges();
                    this.userBilling.subscribe((value) => {

                        const data = value.payload.data();

                        if (data.account_status == "Active") {
                            this.activeAccount = Observable.of(true);
 // observe here 
        observer.next(true)
                            console.log('inside if statement for active = ', this.activeAccount);
                        } else {
                            this.activeAccount = Observable.of(false);
          // observe here 
        observer.next(false)
                            console.log('inside if statement for not active = ', this.activeAccount);
                            this.router.navigate(['resubscribe']);
                        }

                        console.log('userBilling.subscribe = ', this.activeAccount);
                    });

                    console.log('just outside userBilling.subscribe = ', this.activeAccount);

                }

            });
    });


            // When refreshig my page, this returns as undefined.
            // If I navigate to the correct page and work my way through the site it works fine
            // However, refresh returns undefined.

            console.log('out of auth = ', this.activeAccount);

        }
Run Code Online (Sandbox Code Playgroud)

观察我在哪里包装所有Observer.create你可以在这里了解更多信息的地方/sf/answers/3103422801/

然后observer.next将返回您真正希望canActivate钩子做出反应的内容

  // observe here 
        observer.next(true)
    observer.next(false)
Run Code Online (Sandbox Code Playgroud)

  • 太棒了。非常感谢您的快速回复。对于刚接触这个的人来说,这是非常好的信息。我会在 2 分钟内将此标记为答案。 (2认同)
  • 感谢西奥的更新。很高兴得到有人通过资源和代码为问题提供一些真正的教育价值的答复。非常感谢。 (2认同)