我有一个解决方案,可以从服务中调用getSchools()
export abstract class APIResolverService implements Resolve<any> {
constructor(readonly dropdownService:DropDownOptionsService,readonly route:ActivatedRoute) { }
abstract resolve();
}
export class APISchoolsresolve extends APIResolverService{
resolve(){
this.route.queryParams.subscribe(params => {
let district = params['district'];
return this.dropdownService.getSchools(district);
})
}
async getSchools(districtID:number){
return await this.http.get<ISchool[]>(`api/GetSchools/` + districtID).toPromise();
}
this.activatedRoute.data.subscribe((data) =>{
console.log(data);
})
我的组件中的数据是未定义的,因为在我的解析器获取数据之前会调用它。
我该如何解决?
答案 0 :(得分:2)
您的resolve方法不返回任何内容。因此,路由器无需等待任何问题解决:它可能订阅或等待什么?
此外,resolve方法采用ActivatedRouteSnapshot
。那是您应该从中获取districtID的地方。
解析器的外观如下:
export class APISchoolsresolve implements Resolve<Array<ISchool>> {
constructor(private dropdownService: DropdownService) { }
resolve(route: ActivatedRouteSnapshot): Promise<Array<ISchool>> {
return this.dropdownService.getSchools(+route.paramMap.get('district'));
}
}