简体   繁体   English

如何在 Angular 中返回可观察对象内的值

[英]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.

相关问题 如何在Angular 2+中将Observable内部的值作为函数返回 - How do I return the value inside of an Observable as the function in Angular 2+ 如何在 Observable 订阅者角度 8 中返回承诺 - How to return a promise inside Observable subscriber angular 8 Angular:如何从一个可观察对象中获取另一个可观察对象的价值 - Angular: How to get value from an observable inside another observable 如何在 Angular/Ionic Observable/Promise 的 AsyncPipe 中返回默认值 - How to return a default value in an AsyncPipe in Angular/Ionic Observable/Promise Angular / Typescript / Firestore-如何在if语句中返回可观察的值 - Angular / Typescript / Firestore - How to return an observable value within an if statement 如何在 Rxjs Angular 中订阅可观察对象 function 时处理错误并返回可观察对象 - How to handle error and return observable while subscribe inside an observable function in Rxjs Angular 如何在TypeScript组件中使用Observable中的值 角度4 - How to use value from Observable inside TypeScript component | Angular 4 内部订阅并在Angular中返回可观察的对象 - Subscribe inside subscribe and return an observable in Angular 如何在订阅中返回 Observable? - How to return an Observable inside a subscription? Angular 9 - 在 Observable 对象数组中获取值 - Angular 9 - Get value Inside Observable Array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM