Hanoch
Hanoch

Reputation: 349

Passing parameters to a service constructor?

I'm trying to instantiate a service in a component. I got this error

Uncaught Error: Can't resolve all parameters for AuthenticationService: (?, [object Object], ?).

This is my service

     @Injectable()
    export class AuthenticationService {

      protected basePath = 'http://localhost:8080/rest';
      public defaultHeaders: Headers = new Headers();
      public configuration: Configuration = new Configuration();

      constructor(protected http: Http, @Optional() @Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (basePath) {
          this.basePath = basePath;
        }
        if (configuration) {
          this.configuration = configuration;
          this.basePath = basePath || configuration.basePath || this.basePath;
        }
      }

    public login(authenticationkey?: string, body?: AuthenticationRequestHolder, extraHttpRequestParams?: any): Observable<AuthenticationBundle> {
        return this.loginWithHttpInfo(authenticationkey, body, extraHttpRequestParams)
          .map((response: Response) => {
            if (response.status === 204) {
              return undefined;
            } else {
              return FlexiCoreDecycl`enter code here`e.retrocycle(response.json()) || {};
            }
          });
      }

// More Code

and this is the component that uses the service

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  constructor(private _authenticationService: AuthenticationService) {
  }

  ngOnInit() {
    this._authenticationService
      .login('', {mail: '[email protected]', password: 'admin'})
      .subscribe(response => {
        console.log(response);
      },
        error => {
        console.log(error);
        });
  }
}

and here is the angular module

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [AuthenticationService],
  bootstrap: [AppComponent]
})
export class AppModule { }

Can anybody help me how to properly wire them up? I'm new for both angular and typescript so, I will appreciate a little bit explanation and pointers.

Upvotes: 4

Views: 9135

Answers (2)

Hanoch
Hanoch

Reputation: 349

There might be another way but here is how I solved it. using FactoryBuider. this is what my app.module lokes like

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppComponent} from './app.component';
import {LoginComponent} from './login/login.component';
import {AuthenticationService, BASE_PATH, Configuration} from '@hanoch/fc_client';
import {Http, HttpModule} from '@angular/http';
import {FormsModule} from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    HttpModule,
    FormsModule
  ],
  providers: [
    Configuration,
    {
      provide: AuthenticationService,
      useFactory: AuthenticationServiceFactory,
      deps: [Http]
    }
  ],
  bootstrap: [AppComponent]
})

export class AppModule {
}

export function AuthenticationServiceFactory(http: Http) {
  return new AuthenticationService(http, 'http://localhost:8080/rest', Configuration);
}

Upvotes: 6

Viktor Lova
Viktor Lova

Reputation: 5034

  1. You need to add HttpModule to your imports in AppModule, because class Http is angular service defined in HttpModule.

    imports: [
       BrowserModule,
       HttpModule
    ],
    
  2. If you want to inject basePath and configuration you should add to providers them:

    providers: [
        {
            provide: BASE_PATH,
            useValue: 'https://google.com' // replace with yours
        }, 
        {
            provide: Configuration,
            useValue: new Configuration() // replace with correct instance
        }
        AuthenticationService
    ]
    

You can read a lot about dependency injection in angular tutorial: Angular: Dependency Injection

Upvotes: 0

Related Questions