【问题标题】:How to import the jwt-decode type definition into Typescript (Ionic 2)如何将 jwt-decode 类型定义导入 Typescript (Ionic 2)
【发布时间】:2017-06-08 16:46:40
【问题描述】:

就在我认为我可以控制 Typescript 中的打字时,我遇到了相反的情况。

这次我尝试使用jwt-decode。我已经通过命令typings i dt~jwt-decode --save

安装了类型定义

两个问题

1.当我查看 index.d.ts 时,我看到以下内容

    declare module 'jwt-decode' {
        namespace JwtDecode {
        interface JwtDecodeStatic {
            (token: string): any;
        }
    }

    var jwtDecode: JwtDecode.JwtDecodeStatic;
    export = jwtDecode;
    export as namespace jwt_decode;
    }

IDE(VS Code)显示错误“[ts]全局模块导出可能只出现在顶层”在最后一行export as namespace jwt_decode;

2.如何导入?

我尝试导入语句..

   import { ??  } from 'jwt-decode';

但我看不到要导入的内容。

我能找到的其他(许多)示例似乎都没有帮助。一定很简单,就是不懂语法。

提前感谢您的帮助。

[更新] 经过一番阅读,看起来打字已经被仅使用 npm 取代了..

所以我尝试了

npm install --save jwt-decode
npm install --save @types/jwt-decode

// and import via
import * as JWT from 'jwt-decode';

但仍然无法正确导入。

[更新2] 我可以添加语句let t = jwt-decode("aaa"); 并查看签名,但是出现以下IDE错误

[ts] 'jwt_decode' refers to a UMD global, but the current file is a module. Consider adding an import instead.

【问题讨论】:

  • import * as JWT from 'jtw-decode';最后一行应该是jwt-decode 这是一个错字吗?
  • 是的,是错字。修复并添加了更多信息
  • 这 3 个对我有用 npm install --save jwt-decode npm install --save @types/jwt-decode // 并通过 import * 作为 JWT from 'jwt-decode' 导入;跨度>

标签: angular typescript ionic2 typescript-typings


【解决方案1】:

对我有用的解决方案是:

npm install --save jwt-decode
npm install --save @types/jwt-decode

// and import via
import * as JWT from 'jwt-decode';

// use JWT() for decode. Not jwt-decode() !!
let t = JWT(token);

【讨论】:

  • 如何从 jwt 令牌中获取特定值。是否有可能在角度。以上是仅打印对象。我需要从 t 中获取值。我们如何才能做到这一点。
  • 变量 t 可以像其他对象一样访问它的所有属性。
  • 如果您期望某些属性,您可以定义t: JWTPayload 并创建一个接口JWTPayload,其中包含解码令牌所具有的所有属性。
猜你喜欢
  • 2017-02-10
  • 2019-04-30
  • 2020-01-28
  • 1970-01-01
  • 2017-06-13
  • 2016-11-17
  • 2017-10-18
  • 2017-12-19
  • 2019-02-05
相关资源
最近更新 更多