我使用了 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)
我在我的离子/角度/电容器应用程序中执行此操作以解码 jwt 令牌:
npm install @types/jwt-decode
添加导入:
import jwt_decode from 'jwt-decode';
在您的代码中(tok 是您要解码的令牌):
const tokenDecoded = jwt_decode(tok)
我已将导入更改为
import { jwtDecode } from 'jwt-decode';
并且成功了!