【问题标题】:Getting error in Jwthelperservice在 Jwthelperservice 中出现错误
【发布时间】:2018-03-05 11:46:48
【问题描述】:

我对 Angular 很陌生,我获取了代码:

网址 = https://github.com/DavideViolante/Angular-Full-Stack

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

import { JwtHelperService } from '@auth0/angular-jwt';

import { UserService } from './user.service';
import { HttpClient } from '@angular/common/http';
import { User } from '../shared/models/user.model';

import 'rxjs/add/operator/map';

@Injectable()
export class AuthService {
  loggedIn = false;
  isAdmin = false;

  currentUser: User = new User();

  constructor(private userService: UserService,
              private http: HttpClient,
              private router: Router,
              private jwtHelper: JwtHelperService) {
    const token = localStorage.getItem('token');
    if (token) {
      const decodedUser = this.decodeUserFromToken(token);
      this.setCurrentUser(decodedUser);
    }
  }

  login(emailAndPassword) {
    return this.userService.login(emailAndPassword).map(
      res => {
        localStorage.setItem('token', res.token);
        const decodedUser = this.decodeUserFromToken(res.token);
        this.setCurrentUser(decodedUser);
        return this.loggedIn;
      }
    );
  }

  decodeUserFromToken(token) {
    return this.jwtHelper.decodeToken(token).user;
  }
}

我正在使用 Visual Studio 代码,它没有抛出任何语法错误。

但在控制台中出现错误

Error: StaticInjectorError(AppModule)[AuthService -> JwtHelperService]: 
  StaticInjectorError(Platform: core)[AuthService -> JwtHelperService]: 
    NullInjectorError: No provider for JwtHelperService!
    at _NullInjector.get (core.js:994)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)
    at StaticInjector.get (core.js:1102)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)

【问题讨论】:

  • 您导入 angular-jwt 这是用于 angularjs 应用程序。改为安装 angular2-jwt
  • 在上面安装时收到消息“angular2-jwt@0.2.3 需要 @angular/core@^2.0.0||^4.0.0 的对等体,但没有安装”
  • 你试过了吗:import { JwtHelperService } from '@auth0/angular-jwt';npm install @auth0/angular-jwt --save之后
  • 它现在修复了...github.com/DavideViolante/Angular-Full-Stack/issues/173我已经关闭它

标签: angular angular2-jwt


【解决方案1】:

您必须将 AuthGuard 添加到 app.module 中的提供程序列表中:

  providers: [
    AuthGuard
  ],

【讨论】:

    【解决方案2】:

    这个错误只是因为你没有在 app 模块文件中添加 jwtmodule。所以在这里你需要添加它的配置所以,请在你的 app.module.ts 文件中添加以下代码:

    import { JwtHelperService, JwtModule } from '@auth0/angular-jwt';
    ...
    @NgModule({
    imports: [
         ...
        JwtModule.forRoot({
        config: {
          tokenGetter: function  tokenGetter() { 
          return localStorage.getItem('token');
          } 
       }
     });
     ...
     ],
     ...
     })
    

    【讨论】:

    • 不鼓励仅使用代码的答案。请单击编辑并添加一些单词来总结您的代码如何解决问题,或者解释您的答案与之前的答案/答案有何不同。谢谢
    • 现在,可以吗? @尼克
    • 谢谢 - 你让它变得更有用了。
    猜你喜欢
    • 2011-10-03
    • 2014-09-26
    • 2018-08-11
    • 2011-05-07
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多