如何解决在 app.module.ts 加载之前调用函数以获取 JSON 对象Angular
我的 app.module.ts 文件是:-
import { MsalModule,MsalInterceptor } from '@azure/msal-angular';
import getConfig from './env';
var config_values = getConfig(); // we are calling the function here,but it takes time to complete
console.log("here",config_values);
const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1;
@NgModule({
declarations: [
...
...
...
],imports: [
...
...
...
MsalModule.forRoot({
auth: config_values,// we want the json values here which are returned by the above function.
cache: {
cacheLocation: 'localStorage',storeAuthStateInCookie: isIE,// Set to true for Internet Explorer 11
},},{
popUp: !isIE,consentScopes: [
'user.read','openid','profile',],unprotectedResources: [],protectedResourceMap: [
['https://graph.microsoft.com/v1.0/me',['user.read']]
],extraQueryParameters: {}
})
],providers: [
{ provide : HTTP_INTERCEPTORS,useClass : htt,multi : true },{ provide: HTTP_INTERCEPTORS,useClass: MsalInterceptor,multi: true},GlobalsService
],bootstrap: [component]
})
export class AppModule {
}
Environment.ts 文件是:-
export function getConfig() {
try{
let request = new XMLHttpRequest();
var config;
request.open('GET',"URL",false);
request.send(null);
if (request.status === 200) {
console.log(request.responseText);
config = request.responseText;
}
return JSON.parse(config);
}
catch(e){
console.log(e);
}
}
export default getConfig;
由于是外部调用,获取数据需要时间,但是模块加载之前会抛出错误,所以我们想要一个解决方案,它要么等待外部数据加载,要么在获取数据后加载SSO 工作正常。 我们想在文件加载之前获取配置值。 有人可以帮忙吗!?
解决方法
您可以使用 platformBrowserDynamic
动态获取 MSAL 的配置值。请参阅:https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/v1-docs/configuration.md#platformbrowserdynamic
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。