注入可能なサービスの洞察を使用するのに苦労しています角度5の別の注入可能なサービスここに私のcrudService.tsがあります:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class CrudService
{
constructor(
private http: HttpClient
) { }
postItem(url: any, body: any, headers?: any, params?: any)
{
return this.http.post<any>(url,
body,
{
headers, params
}
);
}
}
これが私の authentication.service.ts で、crudService.ts ファイルを挿入しています。
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpClientModule } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { CrudService } from "../common/services/crudservice";
@Injectable()
export class AuthenticationService {
constructor(
private http: HttpClient,
private crudService: CrudService
) { }
login(username: string, password: string) {
let url = "example";
let body = "example";
let headers = "example";
return this.crudService.postItem(url, body, headers);
}
}
および洞察 app.module.ts ファイル CrudService クラスのみをインポートします。
import { CrudService } from "./common/services/crudService";
@NgModule({
imports: [
...
],
declarations: [
...
],
providers: [
...,
CrudService
],
bootstrap: [AppComponent]
})
私の CrudService 注入可能なエクスポート クラス インサイト app.module.ts ファイル。実際には、2つのクラスをインポートするようなさまざまなシナリオを試しました: CrudService と AuthenticationService の洞察 app.module.ts を同時に、他のアプローチで... コンパイル中にエラーはありませんが、アプリケーションの起動時にエラーが発生します:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[LoginComponent -> AuthenticationService]:
StaticInjectorError(Platform: core)[LoginComponent -> AuthenticationService]:
NullInjectorError: No provider for AuthenticationService!
Error: StaticInjectorError(AppModule)[LoginComponent -> AuthenticationService]:
StaticInjectorError(Platform: core)[LoginComponent -> AuthenticationService]:
NullInjectorError: No provider for AuthenticationService!
ある @Injectable() クラスのインサイトを使用して別の @Injectable() クラスを使用し、そのロジック インサイト app.module.ts を Angular 5 に登録することに関連する問題を誰かが既に解決している場合は、リンクまたは可能性のあるシナリオを送ってください。 @Injectable() クラスを正常にコンパイル、実行、および使用するには、次の手順に従います。正確なものが必要な場合は、お問い合わせください。詳細をお知らせします。ありがとう
PS My AuthenticationService はインサイト LoginComponent クラスに注入されます:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { AlertService, AuthenticationService } from '../_services/index';
import { CrudService } from "../common/services/crudservice";
@Component({
moduleId: module.id,
templateUrl: 'login.component.html'
})
export class LoginComponent implements OnInit {
model: any = {};
loading = false;
returnUrl: string;
constructor(
private route: ActivatedRoute,
private router: Router,
private authenticationService: AuthenticationService) { }
ngOnInit() {
}
login() {
this.loading = true;
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(
resp => {
},
error => {
});
}
}