使用route resolver可以相对轻松地完成此操作。
事情是这样的。在路由配置中,提供路由路径、组件和
resolver
.
const routes = [{
path: 'projects/:projectNumber',
component: DestinationComponent,
resolve: {
project: ProjectResolver
},
}];
提供您提到的解析器
. 例如,像这样:
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
providers: [ProjectResolver],
})
export class ProjectDetailsModule {}
但是这个项目解析器是什么?它只是一个可注入类,实现Angular的Resolve接口,该接口位于Angular的
resolve()
方法以数据、承诺或可观察的形式返回数据。看起来像这样:
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
// You will also need your service
import { ProjectService } from './project.service';
@Injectable()
export class ProjectResolver implements Resolve {
// Inject it with where the data comes from
constructor(private projectService: ProjectService) {}
// Now implement the resolve interface. Angular will give you the route,
// from which you can read the parameter.
resolve(route: ActivatedRouteSnapshot): Observable<Project> {
const projectNumber = route.snapshot.params['projectNumber'];
return this.projectService.getProjectByNumber(projectNumber);
}
}
现在,我们必须为您的服务修复一点问题-http方法通常是异步的,因此您必须返回可观察的:
// I'll change the return type, from Project, to Obsevable<Project>
getProjectByNumber(projectNumber: string): Observable<Project> {
return this.http.get(`${ httpBaseUrl }/project/getProject/${ projectNumber }`, httpOptions)
// Instead of subscribe, you'll use a pipe, to map. So that others can actually subscribe (in our case the Angular router)
.pipe(
map(data => {
this.logger.info("Received data, processing...");
project = data['projectDto'];
return project;
// This whole `map(...)` part could have been a one-liner:
// map(data => data.projectDto)
// so alltogether:
// this.http.get().pipe( map(data => data.projectDto), catchError(this.logger.error));
},
catchError(err => this.logger.error('Error getting project:', err.message)),
);
}
// in the component
ngOnInit() {
this.activatedRoute.data.subscribe(project => this.project = project);
// or something like this for immediate value:
// this.data = this.route.snapshot.data;
}
请注意,这将保持页面处于“加载”状态,直到数据实际返回。如果您想并行获取数据,这是一个不同的答案,但也可以这样做。