我是Angular 4中Observables的新手,并尝试实现以下内容
这是我的路线
{ path: 'files', component: FilesComponent, resolve: { files: FilesResolver }}
在我的FilesResolver中我有解决方法是。我调用多个服务来获取某些身份验证令牌,最后调用httpservice(我的一个实现HttpClient模块的服务)
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
this.sharedService.workspace$.mergeMap(
workspace => {
this.workspace = workspace;
return this.nodeService.getNode(this.workspace.home_node_id)
}).mergeMap(nodeData => {
this.node = nodeData.body;
return this.nodeService.getToken(this.node.access_key, 'user');
}).mergeMap(tokenData => {
this.nodeToken = tokenData.body.access_token;
return this.httpService.get(this.node.url + 'files/' + this.workspace.home_file_id + '/files', false, null,
{
'Authorization': "Bearer " + this.nodeToken,
'X-Aspera-AccessKey': this.node.access_key
}
)
});
}
HttpService代码
public get( url: string, api: boolean, authType?: string, headers?: object ) {
return this.httpClient.get( this.makeUrl( url, api ), this.createHeaders( headers, authType ) );
}
最后我在我的FilesComponent中订阅了
constructor(
private router: Router,
private route: ActivatedRoute
) {
this.route.data.subscribe( data => {
console.log(this.route);
console.log(data);
//this.files = data.files.body;
});
console.log
中的我的数据为undefined
。我无法弄清resolve
方法中的错误。我需要从httpService返回的数据在我的解析器中获取到我的组件。
我能够获得进行httpService get调用所需的所有数据。但是,当我检查网络选项卡时,永远不会进行http get调用。非常感谢任何帮助。