[英]How to return a value inside an observable in Angular
I need a response from an observable to set values before the MSAL authenticates.在 MSAL 进行身份验证之前,我需要来自可观察对象的响应来设置值。
Is there a way to return value inside the getAuthenticationConfiguration() observable?有没有办法在 getAuthenticationConfiguration() observable 中返回值?
How can I return values after the values are received inside the observable.在可观察对象中接收到值后,如何返回值。
ps It is not possible to return inside the subscribe function. ps subscribe function里面是不可能返回的。
export function MSALInstanceFactory(service: AzureService): IPublicClientApplication {
service.getIdentityManagerApiRestService().getAuthenticationConfiguration().subscribe(response => {
return new PublicClientApplication({
auth: response.authenticationConfiguration, <-------- I want this
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
storeAuthStateInCookie: isIE, // set to true for IE 11. Remove this line to use Angular Universal
}
});
})
}
@NgModule({
declarations: [
AzureComponent
],
exports: [
AzureComponent
],
providers: [
{
provide: MSAL_INSTANCE,
useFactory: MSALInstanceFactory,
deps: [AzureService]
}
]
})
export class AzureModule { }
I would try to pipe the response (untested code):我会尝试 pipe 响应(未经测试的代码):
service
.getIdentityManagerApiRestService()
.getAuthenticationConfiguration()
.pipe(switchMap((res) => res.authenticationConfiguration))
.subscribe((config) => {
return new PublicClientApplication({
auth: config,
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
storeAuthStateInCookie: isIE, // set to true for IE 11. Remove this line to use Angular Universal
},
});
});
I believe your getAuthenticationConfiguration()
should look like this to return some response from it:我相信您的
getAuthenticationConfiguration()
应该看起来像这样以从中返回一些响应:
getAuthenticationConfiguration() { return authenticationConfiguration$.pipe(map(data) => data.authenticationConfiguration)) }
and when you subscribe to it, you can do this:当你订阅它时,你可以这样做:
service.getIdentityManagerApiRestService().getAuthenticationConfiguration().subscribe(response => { if(response.authenticationConfiguration) { return new PublicClientApplication({ auth: response.authenticationConfiguration, cache: { cacheLocation: BrowserCacheLocation.LocalStorage, storeAuthStateInCookie: isIE, } }); } })
So basically you can add a check on response for the expected property in the response, once received then only do further code execution.所以基本上你可以在响应中为预期的属性添加一个响应检查,一旦收到然后只做进一步的代码执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.