加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – ERROR错误:没有JwtHelper的提供者

发布时间:2020-12-17 17:05:05 所属栏目:安全 来源:网络整理
导读:我是Angular2的新手,我正在使用jwthelper来编码jwt令牌并提取细节. import { Injectable } from '@angular/core';import { JwtHelper } from 'angular2-jwt';@Injectable()export class SessionService { constructor(private jwtHelper: JwtHelper){} getUs
我是Angular2的新手,我正在使用jwthelper来编码jwt令牌并提取细节.

import { Injectable } from '@angular/core';

import { JwtHelper } from 'angular2-jwt';

@Injectable()
export class SessionService {

  constructor(private jwtHelper: JwtHelper){}

  getUser(){
    var token = JSON.parse(localStorage.getItem('currentUser'));
    return this.jwtHelper.decodeToken(token.token);
  }
}

找不到以下错误的解决方案,并告诉我这里有什么不对.

ERROR Error: No provider for JwtHelper!

解决方法

这是因为您试图在组件/服务的构造函数中注入JwtHelper.这仅适用于提供商.

根据using jwthelper in components部分,您必须创建一个新对象并使用它.

在您的服务中,

constructor(){} //remove from constructor.

  getUser(){
    let jwtHelper: JwtHelper = new JwtHelper();
    var token = JSON.parse(localStorage.getItem('currentUser'));
    return this.jwtHelper.decodeToken(token.token);
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读