You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular9中@auth0/angular-jwt无法正常工作的问题求助

解决Angular9中@auth0/angular-jwt配置报错问题

核心原因:版本不兼容

Angular9需要适配特定版本的@auth0/angular-jwt,最新版包仅支持Angular10+,直接安装会导致依赖冲突。

解决方案步骤:

  1. 安装兼容版本的包
    先卸载当前包,再安装适配Angular9的5.x版本:
npm uninstall @auth0/angular-jwt
npm install @auth0/angular-jwt@5.2.0 --save
  1. 校验tokenGetter函数定义
    确保tokenGetter函数符合要求,需返回令牌字符串或对应的异步类型,示例:
// 在module.ts文件内定义或单独文件导出后导入
export function tokenGetter() {
  return localStorage.getItem('access_token');
}

注意函数需在JwtModule.forRoot配置中可访问,避免作用域问题。

  1. 修正配置项格式
  • allowedDomains仅填域名,无需带http/https前缀,保持和实际API域名一致
  • disallowedRoutes可使用完整URL或相对路径,确保与需排除的请求路径完全匹配
    修正后的配置示例:
imports: [
  HttpClientModule,   
  BrowserAnimationsModule,

  JwtModule.forRoot({
    config: {
      tokenGetter: tokenGetter,
      allowedDomains: ["example.com"],
      disallowedRoutes: ["https://example.com/examplebadroute/"],
    },
  }),
]
  1. 确认依赖导入顺序
    确保HttpClientModule在JwtModule之前导入,因为JWT拦截器依赖HttpClient。

内容的提问来源于stack exchange,提问作者jhon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:40:26