There is no provider for AuthHttp! on angular2 -jwt

im really new on angular 2 and i want to use JWT in my project. So I follow the instructions where the official angular2 -jwt page is given using the basic configuration. I create a file called auth.module.ts with the following code:

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 {}

The next step is to send an authenticated request. I use a component in which I put a button that calls a function that executes the code suggested on the page:

File: 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");
    }
}

When I enter this component, I have an error:

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

Any idea that I can solve this? I am really new to Angular 2, thanks for your help!

+3
1

, , AuthHttp. , AuthModule , CalendarioComponent ?

- :

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

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

Source: https://habr.com/ru/post/1675864/


All Articles