角度6:无法从路由解析器获取数据

时间:2018-10-08 17:33:44

标签: angular angular6 angular-router-guards

我有一个路由配置,允许用户根据类似这样的标准查看最近完成的活动:

{ 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
    });
}

我无法理解为什么数据没有到达这里。谁能指导我?预先感谢。

1 个答案:

答案 0 :(得分:0)

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

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