如何从 AngularFirestore 获取 return 可观察值并解析我的路由器防护
How to get a return an observable from AngularFirestore and resolve my router guard
问题:我如何调用 firestore 并使用从 firestore returned 的有效负载解析我的路由器防护?
问题:我的路由解析守卫没有得到解析,即使来自 firestore 的值正在被 returned 并被定义。
import {
ActivatedRouteSnapshot,
Resolve,
RouterStateSnapshot,
} from '@angular/router';
import { AngularFirestore } from 'angularfire2/firestore';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
@Injectable()
export class RouteResolve implements Resolve<any> {
constructor(private db: AngularFirestore) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
return this.db
.collection('routes')
.doc('work')
.valueChanges()
.pipe(
map((item) => {
if (item) {
console.log('items', item) // this logs just fine.
return item; // this doesen't resolve the router guard.
}
}));
}
}
一切正常,但已完成解决。如果我只是 return true,则解析会按预期工作。
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return true; // this will resolve just fine
}
将 pipe(first())
添加到您的 valueChanges()
可观察对象。 Angular 路由器等到 observable 完成,这样做可以确保 observable 在第一次值更改后完成。
return this.db
.collection('routes')
.doc('work')
.valueChanges()
.pipe(first())
.pipe(
map((item) => {
if (item) {
console.log('items', item) // this logs just fine.
return item; // this doesen't resolve the router guard.
}
}));
问题:我如何调用 firestore 并使用从 firestore returned 的有效负载解析我的路由器防护?
问题:我的路由解析守卫没有得到解析,即使来自 firestore 的值正在被 returned 并被定义。
import {
ActivatedRouteSnapshot,
Resolve,
RouterStateSnapshot,
} from '@angular/router';
import { AngularFirestore } from 'angularfire2/firestore';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
@Injectable()
export class RouteResolve implements Resolve<any> {
constructor(private db: AngularFirestore) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
return this.db
.collection('routes')
.doc('work')
.valueChanges()
.pipe(
map((item) => {
if (item) {
console.log('items', item) // this logs just fine.
return item; // this doesen't resolve the router guard.
}
}));
}
}
一切正常,但已完成解决。如果我只是 return true,则解析会按预期工作。
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return true; // this will resolve just fine
}
将 pipe(first())
添加到您的 valueChanges()
可观察对象。 Angular 路由器等到 observable 完成,这样做可以确保 observable 在第一次值更改后完成。
return this.db
.collection('routes')
.doc('work')
.valueChanges()
.pipe(first())
.pipe(
map((item) => {
if (item) {
console.log('items', item) // this logs just fine.
return item; // this doesen't resolve the router guard.
}
}));