Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

Angular8 MergeMap 串联请求中,第三级如何拿到第一级的值?

解决了。httpthis 拿出来用,存储 token 就可以了。

MergeMap 多次请求,第一次拿到 token,想在第二次以后的调用中使用.
(第二次MergeMap) tokenRes 如何取值 ?

getInfo(): Observable<any> {
const httpThis = this;
const headers = new HttpHeaders().set('Content-Type', 'application/json')
.set('Content-Type', 'application/x-www-form-urlencoded');
const body = '<tsRequest>' +
'<credentials name="ywm" password="ywm" >' +
'<site contentUrl=""/>' +
'</credentials>' +
'</tsRequest>';
const hdr = { headers };
return httpThis.http.post(this.uri_tableau + 'api/3.8/auth/signin', body, hdr)
.pipe(map(
(res: {credentials: Token}) => {
return res.credentials.token;
}),

mergeMap((tokenRes: string) => {
const headersSite = new HttpHeaders().append('X-Tableau-Auth', tokenRes)
const hdrSite = { headers: headersSite };
return httpThis.http.get(this.uri_tableau + 'api/3.9/sites', hdrSite)
.pipe(map(
(sitesRes: {sites: Sites}) => {
return sitesRes.sites.site[0].id;
}
))
}),

mergeMap((sitesId: string) => {
const headersProject = new HttpHeaders().append('X-Tableau-Auth', tokenRes)//*此处 tokenRes 拿不到值
const hdrProject = { headers: headersProject };
return httpThis.http.get(this.uri_tableau + 'api/3.9/sites/' + sitesId + '/projects', hdrProject)
.pipe(map(
(projectRes: {projects: Projects}) => {
return projectRes.projects.project;
}
))
})
);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

经测试,http 不用 this 转化,内层拿得到。

 const that = this;
 let tokenQ = '';

在第一次拿到 token 后:

 tokenQ = res.credentials.token;

在其它地方,就可以 that.tokenQ 了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...