angular - 没有 AuthHttp 的提供者!在 angular2-jwt 上

标签 angular authentication jwt angular2-jwt

我是 Angular 2 的新手,我想在我的项目中使用 JWT。所以我完全按照 the official page of angular2-jwt 给出的说明进行操作使用基本配置。 我使用以下代码创建一个名为 auth.module.ts 的文件:

import { NgModule } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig } from 'angular2-jwt';

function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig(), http, options);
}

@NgModule({
  providers: [
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [Http, RequestOptions]
    }
  ]
})
export class AuthModule {}

下一步是发送经过身份验证的请求。我使用了一个组件,我在其中放置了一个按钮,该按钮调用一个函数来执行页面上建议的代码:

文件:calendario.components.ts

import {Component, OnInit,trigger,state,style,transition,animate,keyframes, group} from '@angular/core';
import initDemo = require('../../../assets/js/charts.js');
import initNotify = require('../../../assets/js/notify.js');
import { AuthHttp } from 'angular2-jwt';

declare var $:any;

@Component({
    moduleId: module.id,
    selector: 'calendario',
    templateUrl: 'calendario.component.html'

})

export class CalendarioComponent{
    thing: any;
    constructor(public authHttp: AuthHttp) {}
    obtenerDatos() {
        this.authHttp.get('http://localhost/autorepuestos/web/app_dev.php/api/conjunto')
      .subscribe(
        data => this.thing = data,
        err => console.log(err),
        () => console.log('Request Complete')
      );
        console.log("Hola");
    }
}

当我输入这个组件时出现错误:

EXCEPTION: Uncaught (in promise): Error: No provider for AuthHttp!
Error: DI Error

我能解决这个问题吗?我真的是 Angular 2 的新手,感谢您的帮助!

最佳答案

看起来您已经创建了一个将提供此 AuthHttp 类的模块。您是否确定 AuthModuleCalendarioComponent 所属模块的导入中?

像这样:

import { AuthModule } from '...';
import { CalendarioComponent } from '...';

@NgModule({
    imports: [AuthModule],
    declarations: [CalendarioComponent]
})
export class SomeModule {}

关于angular - 没有 AuthHttp 的提供者!在 angular2-jwt 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42962519/

相关文章:

Spring OAuth/JWT 从访问 token 获取额外信息

jwt - 使用 OpenId (Cognito) 进行身份验证后,如何在 Blazor WebAssembly 中获取 id_token?

Angular 2 OpaqueToken

authentication - Wildfly 8 基本身份验证

python - 使用 GRequest 进行身份验证或基本身份验证

security - 如何成为 SAML 服务提供商

node.js - 如何将请求 header 传递给 graphql 解析器

angular - 如何在 Angular 2 中使用 Protractor 测试 ngx-toastr

angular - 自定义组件绑定(bind): No value accessor for ''

javascript - 使用服务提供者(typescript/Angular 2)在 ionic 2 View 中恢复并显示 json 数据(对象、数组)