2017-10-26 4 views
0

コンポーネントでサービスをインスタンス化しようとしています。私はこのエラーを得たサービスコンストラクタにパラメータを渡しますか?

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

これは私のサービス

 @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 

であり、これはサービスに

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

を使用するコンポーネントであり、ここで、角度モジュール

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

誰も助けてくれますか?適切に配線しますか?私は角と形の両方のために新しいので、私は少し説明とポインタを感謝します。

答えて

1

を別の方法があるかもしれませんが、ここで私はそれをどのように解決しましたか? FactoryBuiderを使用します。 これは

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. あなたはクラスHttpはHttpModuleをで定義された角度のサービスですので、AppModuleであなたの輸入にHttpModuleを追加する必要があります。

    imports: [ 
        BrowserModule, 
        HttpModule 
    ], 
    
  2. あなたがbasePathを注入したいとconfiguration場合は、プロバイダにそれらを追加する必要があります

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

あなたは角度チュートリアルで依存性注入について多くを読むことができます:Angular: Dependency Injection

+0

のように私のapp.module lokesは、私は私がにサービスを登録しようとしているときに問題が起こっていると信じて、あなたの提案を追加し、いくつかの研究をしましたが、問題はまだそこにあるものですプロバイダ。私はそれらを並べ替えることを試みたが、何も変わらなかった。 – Hanoch

+0

@Hanochプロジェクトをいくつかのgitリポジトリにアップロードできますか? –

+0

できません。私が持っている問題は、プロバイダに "AuthenticationService"を登録できないということです。私は新しいプロジェクトを開始した後、サービスを追加して登録しようとしました。私は別のコンポーネントでそれを使用しようとしていません。 – Hanoch

関連する問題