2

我有一个路由配置,它允许用户根据如下标准查看最近完成的活动:

{ path: 'workspace', component: WorkspaceComponent, children: [
{ path: 'upload', component: UploadComponent },
{ path: 'verify', component: VerifyComponent, resolve:{dataObject: RecentActivityResolver}}]}

解析器从存储在服务类中的数组列表中返回一个对象。我没有直接访问我的 VerifyComponent 中的服务类,因为我打算很快从数组更改为 HTTP 请求。

@Injectable()
export class RecentActivityResolver implements Resolve<RecentActivityModel>{
constructor(private recentActivity: RecentActivityService){}
resolve(activatedRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<RecentActivityModel> |
Promise<RecentActivityModel> | RecentActivityModel {
    console.log(activatedRoute.queryParams['id']); // gives me the correct result here
    return this.recentActivity.recentActivities[+activatedRoute.queryParams['id']];
    }
}

在我的子组件上,我从可观察的数据中获取详细信息,如下所示:

ngOnInit(){
    this.activatedRoute.data.subscribe((data: Data) => {
        console.log(data['dataObject']); // undefined
        this.displayActivity = data['dataObject'];
    });
    this.activatedRoute.queryParams.subscribe((data: Data) =>{
        console.log(data['id']); // correct data here
    });
}

我无法理解为什么数据不来这里。谁能指导我?提前致谢。

4

1 回答 1

0

奇怪的是,解决方案/解决方法是将解析器移至父级。但我真的很想知道为什么会这样。

{ path: 'workspace', component: WorkspaceComponent, resolve:{dataObject: RecentActivityResolver}, children: [
{ path: 'upload', component: UploadComponent },
{ path: 'verify', component: VerifyComponent }
]}
于 2018-10-09T02:20:11.473 回答