为什么 AngularFire observable 在路由解析器中没有完成? - 即使在 .pipe(take(1)) 之后
Why AngularFire observable does not complete in route resolver? - even after .pipe(take(1))
在我的 Angular 应用程序中,我使用路由解析器到 return 一个 'restaurant' 对象。
但是,当我加载路线时,可观察对象似乎没有完成。我添加了 .pipe(take(1))
以确保它完成,但它不起作用。
谁能帮我找出我做错了什么?
restaurant.resolver.ts
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { TransferState } from "@angular/platform-browser";
import { Restaurant } from '../restaurants/Interfaces.model';
import { AngularFirestore } from '@angular/fire/firestore';
import { first, take } from 'rxjs/operators';
import { OrderFormService } from '../restaurants/order-form.service';
@Injectable()
export class RestaurantResolver implements Resolve<Restaurant> {
constructor(
public afs: AngularFirestore,
private transferState: TransferState,
public orderForm: OrderFormService,
@Inject(PLATFORM_ID) private platformId) {
}
resolve(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<any> {
let restaurant = this.afs.doc('restaurants/thaiHouse').valueChanges().pipe(take(1))
console.log('e', restaurant)
return restaurant
}
}
这是 Angularfire 库的一个已知问题。希望它能尽快修复。请参阅此处记录的问题:https://github.com/angular/angularfire/issues/2695
在我的 Angular 应用程序中,我使用路由解析器到 return 一个 'restaurant' 对象。
但是,当我加载路线时,可观察对象似乎没有完成。我添加了 .pipe(take(1))
以确保它完成,但它不起作用。
谁能帮我找出我做错了什么?
restaurant.resolver.ts
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { TransferState } from "@angular/platform-browser";
import { Restaurant } from '../restaurants/Interfaces.model';
import { AngularFirestore } from '@angular/fire/firestore';
import { first, take } from 'rxjs/operators';
import { OrderFormService } from '../restaurants/order-form.service';
@Injectable()
export class RestaurantResolver implements Resolve<Restaurant> {
constructor(
public afs: AngularFirestore,
private transferState: TransferState,
public orderForm: OrderFormService,
@Inject(PLATFORM_ID) private platformId) {
}
resolve(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<any> {
let restaurant = this.afs.doc('restaurants/thaiHouse').valueChanges().pipe(take(1))
console.log('e', restaurant)
return restaurant
}
}
这是 Angularfire 库的一个已知问题。希望它能尽快修复。请参阅此处记录的问题:https://github.com/angular/angularfire/issues/2695