til*_*lly 6 javascript angular
我正在尝试用jwt-decode解码我的令牌,但我不能.它给了我以下错误.有谁知道为什么?
错误:未捕获(在承诺中):TypeError:jwt_decode_1.default不是函数TypeError:jwt_decode_1.default不是RoleGuardService.canActivate中的函数(role-guard.service.ts?d7c4:19)
import jwt_decode from 'jwt-decode';
canActivate(route: ActivatedRouteSnapshot): boolean {
// this will be passed from the route config
// on the data property
const expectedRole = route.data.expectedRole;
const token = localStorage.getItem('token');
// decode the token to get its payload
const tokenPayload = jwt_decode(token);
console.log(tokenPayload);
if (
!this.auth.isAuthenticated() ||
tokenPayload.role !== expectedRole
) {
this.router.navigate(['login']);
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
pio*_*o90 14
我认为你应该像这样导入它:
import * as jwt_decode from 'jwt-decode';
Run Code Online (Sandbox Code Playgroud)
根据文档+互联网搜索,正确的方法是:
1.安装包+类型
npm install --save jwt-decode
npm install --save @types/jwt-decode
Run Code Online (Sandbox Code Playgroud)// @ts-ignore import jwt_decode from "jwt-decode";
您还可以在 tsconfig.json 上添加规则,但有 1 次例外:)
| 归档时间: |
|
| 查看次数: |
4161 次 |
| 最近记录: |