2017-10-26 4 views
0

Ich versuche, einen Dienst in einer Komponente zu instanziieren. Ich habe diesen FehlerParameter an einen Dienstkonstruktor übergeben?

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

Das ist mein 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 

und dies ist die Komponente, die den Dienst

@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); 
     }); 
    } 
} 

verwendet und hier ist das Winkelmodul

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

Kann mir jemand helfen wie richtig verdrahten? Ich bin neu sowohl für eckige als auch für Schreibmaschinen, also werde ich ein wenig Erklärung und Hinweise schätzen.

Antwort

1

Es könnte einen anderen Weg geben, aber hier ist, wie ich es gelöst habe. mit FactoryBuider. das ist, was meine app.module Lokes wie

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); 
} 
0
  1. Sie müssen HttpModule auf Ihre Importe in AppModule hinzuzufügen, weil Klasse Http Winkel Service in Httpmodule definiert ist.

    imports: [ 
        BrowserModule, 
        HttpModule 
    ], 
    
  2. Wenn Sie basePath injizieren und configuration Sie Anbieter hinzufügen sollten sie:

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

Sie können viel über Dependency Injection in Winkel Tutorial lesen: Angular: Dependency Injection

+0

ich Ihren Vorschlag und tat es, um nur einige der Forschung, aber das Problem ist immer noch hinzugefügt glaube, dass ich das Problem geschieht, wenn ich versuche, den Dienst der registrieren Anbieter. Ich habe versucht, sie neu zu ordnen, aber nichts hat sich geändert. – Hanoch

+0

@Hanoch Könnten Sie Ihr Projekt auf ein Git Repo hochladen? –

+0

kann ich nicht. aber das Problem, das ich habe, ist, kann ich "AuthenticationService" in Anbietern nicht registrieren. Ich habe ein neues Projekt gestartet, dann habe ich den Dienst hinzugefügt und versucht, ihn zu registrieren. Ich versuche nicht einmal, es in einer anderen Komponente zu verwenden. – Hanoch