我有这个路由器解析器来从 Google Firestore 中检索数据。我正在尝试使用解析器提前获取数据。当我将调试器置于解析函数时,它会显示从服务器检索到的数据。但它永远不会从 resolve() 方法返回。谁能帮帮我。
路由模块
const routes: Routes = [
{ path: '', component: HomeComponent},
{ path: 'projects', component: ProjectsComponent, resolve: {projectData: ProjectResolver} },
{ path: 'about', component: AboutComponent}
];
应用模块
providers: [
ProjectService,
ProjectResolver
],
解析服务
import { Observable } from 'rxjs';
import { ActivatedRouteSnapshot, RouterStateSnapshot, Resolve } from '@angular/router';
import { Injectable } from '@angular/core';
import { ProjectService } from './project.service';
import { map } from "rxjs/operators";
import { Project } from './project.model';
@Injectable()
export class ProjectResolver implements Resolve<Project[]> {
constructor(private projectService: ProjectService ) {
}
resolve(route: ActivatedRouteSnapshot, router: RouterStateSnapshot) : Observable<Project[]> | any{
return this.projectService.getProjects().pipe(map(project => {
return project //Debugger shows fetched data(Array of projects)
}
));
}
}
项目服务
import { AngularFirestore } from 'angularfire2/firestore';
import { Observable } from "rxjs";
import { Injectable } from "@angular/core";
@Injectable()
export class ProjectService {
constructor(private db: AngularFirestore) {}
getProjects(): Observable<any> {
return this.db.collection('currentProjects').valueChanges();
}
}
** 项目组件(此组件从未被解析器加载 )**
import { Component, OnInit, Injectable } from '@angular/core';
import { Project } from './project.model';
import { ActivatedRoute } from '@angular/router';
import { ProjectService } from './project.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-projects',
templateUrl: './projects.component.html',
styleUrls: ['./projects.component.css']
})
export class ProjectsComponent implements OnInit {
projects: Project[] = [];
constructor(private route: ActivatedRoute, private projectService: ProjectService) {
}
ngOnInit() {
this.route.data.subscribe((data: Project[]) => {
this.projects = data['projectData'];
})
}
}