Angular11项目导入@azure/msal-angular的MSAL_CONFIG提示无导出成员
MSAL_CONFIG导入报错问题原因
你遇到的报错是@azure/msal-angular包版本迭代导致的API变更问题:
MSAL_CONFIG是@azure/msal-angular1.x版本才有的导出成员,你当前使用的^2.0.0版本已经正式移除了该常量,新版本重构了配置注入逻辑,不再通过该常量传递全局MSAL配置。
正确使用方式
- 先移除导入语句中的
MSAL_CONFIG项 - MSAL全局配置统一通过
MsalModule.forRoot()方法传入,示例配置如下:
// 需先从@azure/msal-browser导入依赖 import { PublicClientApplication, InteractionType } from '@azure/msal-browser'; import { MsalBroadcastService, MsalGuard, MsalGuardConfiguration, MsalInterceptor, MsalInterceptorConfiguration, MsalModule } from '@azure/msal-angular'; // NgModule imports 段配置 imports: [ MsalModule.forRoot( // 第一个参数为MSAL实例配置 new PublicClientApplication({ auth: { clientId: '你的应用客户端ID', authority: 'https://login.microsoftonline.com/你的租户ID', redirectUri: window.location.origin }, cache: { cacheLocation: 'localStorage', storeAuthStateInCookie: false } }), // 第二个参数为MsalGuard配置 { interactionType: InteractionType.Redirect, authRequest: { scopes: ['user.read'] } } as MsalGuardConfiguration, // 第三个参数为MsalInterceptor配置 { interactionType: InteractionType.Redirect, protectedResourceMap: new Map([ ['https://graph.microsoft.com/v1.0/me', ['user.read']] ]) } as MsalInterceptorConfiguration ) ]
- 如果你有自定义注入获取MSAL配置的需求,2.x版本提供
MSAL_INSTANCE导出常量,可以直接注入获取初始化完成的MSAL实例,代替原先MSAL_CONFIG的使用场景。
内容的提问来源于stack exchange,提问作者Agnete
相关产品推荐
相关产品推荐

