Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Provider for AuthHttp! Angular2-Jwt provider issue

At least I thought I was providing correctly. Below are the relevant snippets of my app.module file and the service in which I use AuthHttp. I followed the configuration in the ReadMe for creating the factory method to provide for AuthHttp, but there is a persisting issue with it not being recognized in my service. I've read the literature on nested dependency injections, and I feel as though I'm doing things correctly.

app.module.ts

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

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

@NgModule({
    declarations: [
        AppComponent,
        ButtonFormComponent,
...
imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        RouterModule,
        AppRoutingModule
    ],
    providers: [
        {
            provide: LocationStrategy,
            useClass: HashLocationStrategy
        },
        {
            provide: AuthHttp,
            useFactory: authHttpServiceFactory,
            deps: [Http, RequestOptions]
        },

employee.service.ts

import { AuthHttp } from 'angular2-jwt/angular2-jwt';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';

import { ApiSettings } from './api-settings';


@Injectable()
export class EmployeeService {
    api: String;
    auth: String;
    constructor(private http: Http, private authHttp: AuthHttp) {
        this.api = ApiSettings.API;
        this.auth = ApiSettings.Auth;
    }
like image 999
ruhrohraggy Avatar asked Apr 27 '17 22:04

ruhrohraggy


1 Answers

You can get rid of this issue by just using following import in your app.module.ts, here the key import for you is, AUTH_PROVIDERS. Also, make sure you include AUTH_PROVIDERS in the providers array.

    import { AuthHttp, AUTH_PROVIDERS, provideAuth, AuthConfig } from 
    'angular2-jwt/angular2-jwt';

    @NgModule({
       providers: [AUTH_PROVIDERS]
    })
like image 169
Santhosh C Avatar answered Oct 02 '22 15:10

Santhosh C