gpt4 book ai didi

angular - 路由器解析器不呈现组件

转载 作者:太空狗 更新时间:2023-10-29 17:32:46 24 4
gpt4 key购买 nike

我有这个路由器解析器来从 Google Firestore 检索数据。我正在尝试使用 Resolver 提前获取数据。当我将调试器置于解析函数时,它会显示从服务器检索的数据。但它永远不会从 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();
}

}

** 项目组件(此组件从未被 Resolver 加载)**

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'];
})
}

}

最佳答案

问题是 Angular Firebase 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'];
})
}
}

关于angular - 路由器解析器不呈现组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51047800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com