6

我有这个路由器解析器来从 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'];
      })
  }

}
4

2 回答 2

9

问题是 Angular Firebase Observable(event stream) 永远不会像正常的“observable”那样完成,所以它永远不会解决。我用“take”操作符修改了 Observable 以获取第一项并强制完成。

this.db.collection('currentProjects').valueChanges().take(1);

变化:

  • 删除了项目服务
  • 修改后的 ProjectResolver
  • 修改项目组件

我在下面发布了我的问题的解决方案:

项目解决者(工作)

export class ProjectResolver implements Resolve<any> {

    constructor(private db: AngularFirestore) {

    }

    resolve(route: ActivatedRouteSnapshot, router: RouterStateSnapshot) : any{

        return Observable.from(this.db.collection('currentProjects').valueChanges()
            .do(data => {  
                return data
            }).take(1))
    }
}

项目组件(工作)

export class ProjectsComponent implements OnInit {

  private projects: Project[];

  constructor(private route: ActivatedRoute) {

  }

  ngOnInit() {
    this.route.data.subscribe((data) => {
      this.projects = data['projectData'];
    })
  }
}
于 2018-06-27T02:24:55.433 回答
0

获取数据this.route.snapshot.data而不是this.route.data不需要订阅它

ngOnInit() {          
   this.projects = this.route.snapshot.data['projectData'];
}
于 2018-06-26T17:41:20.257 回答