angular - “jwt_decode__WEBPACK_IMPORTED_MODULE_11__ 不是函数”尝试使用 jwt-decode npm 包解码令牌时出现离子/角度/电容器错误
问题描述
我已经使用 jwt-decode 包来解码收到的令牌。
import * as jwt_decode from "jwt-decode";
并在收到有效令牌时,将其称为变量tok。
在解码令牌时
var tokenDecoded = jwt_decode(tok)
我收到以下错误
Unhandled Promise rejection: jwt_decode__WEBPACK_IMPORTED_MODULE_11__ is not a function ; Zone: <root> ; Task: Promise.then ; Value: TypeError: jwt_decode__WEBPACK_IMPORTED_MODULE_11__ is not a function
at AuthService.<anonymous> (auth.service.ts:193)
at Generator.next (<anonymous>)
at fulfilled (tslib.es6.js:71)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Zone.run (zone-evergreen.js:124)
at zone-evergreen.js:855
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559) TypeError: jwt_decode__WEBPACK_IMPORTED_MODULE_11__ is not a function
at AuthService.<anonymous> (http://localhost:8100/main.js:1431:32)
at Generator.next (<anonymous>)
at fulfilled (http://localhost:8100/vendor.js:218136:58)
at ZoneDelegate.invoke (http://localhost:8100/polyfills.js:3397:26)
at Zone.run (http://localhost:8100/polyfills.js:3162:43)
at http://localhost:8100/polyfills.js:3893:36
at ZoneDelegate.invokeTask (http://localhost:8100/polyfills.js:3429:31)
at Zone.runTask (http://localhost:8100/polyfills.js:3206:47)
at drainMicroTaskQueue (http://localhost:8100/polyfills.js:3597:35)
解决方案
我在我的 ionic/angular/capacitor 应用程序中执行此操作以解码 jwt 令牌:
npm install @types/jwt-decode
添加导入:
import jwt_decode from 'jwt-decode';
在您的代码中(tok 是您要解码的令牌):
const tokenDecoded = jwt_decode(tok)
推荐阅读
- python - 如何将请求中的 json 数据转换为 excel 文件?
- python - 1 位图像创建 PIL.Image fromarray 错误
- spring - SpringBoot Junit5在重命名包含@SpringBootApplication注释的类的包时失败
- mysql - 无法连接到 docker 容器中的 MySQL 数据库
- sql - 区分大小写会影响 oracle DB 中的 SQL 解析
- python-3.x - 编写一个程序,要求用户输入一个幂。然后找到 2 的最后一位数的幂?
- html - 将页脚放在最小高度页面的末尾
- javascript - 如何将 Google/sheetsAPI 数据传递到网站?
- pytorch - Pytorch 得到“RuntimeError:CUDA 错误:设备端断言已触发”
- mysql - Laravel Eloquent 中的多级深度 hasMany 关系