即使我的参数与数据库中的参数匹配,我也会在控制台上收到以下错误:- 编译器.js:2175 未捕获的错误:无法解析客户端服务的所有参数:(?(。 client.service.ts:
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class Client {
id: number;
firstName: string;
lastName: string;
emailId: string;
constructor()
constructor(id?: number, firstname?: string, lastname?: string, email?: string) {
this.id = id;
this.firstName = firstname;
this.lastName = lastname;
this.emailId = email;
}
}
export class ClientService {
constructor(private http: HttpClient) { }
saveClient(client: Client): Observable<Client> {
return this.http.post<Client>('http://localhost:9091/clients', client); }}
ajout-clients.component.ts
import {Component, forwardRef, Inject, OnInit} from '@angular/core';
import {Client, ClientService} from '../client.service';
@Component({
selector: 'app-ajout-client',
templateUrl: './ajout-client.component.html',
styleUrls: ['./ajout-client.component.css']
})
export class AjoutClientComponent implements OnInit {
constructor(@Inject(ClientService) private clientService:ClientService)
{this.clientService=clientService; }
client: Client = new Client();
submitted = false;
ngOnInit() {
}
newClient(): void {
this.submitted = false;
this.client = new Client();
}
save() {
this.clientService.saveClient(this.client)
.subscribe(data => console.log(data), error => console.log(error));
this.client = new Client();
}
}
ajout-client.component.html
<h3>Create Client</h3>
<div [hidden]="submitted" style="width: 400px;">
<form (ngSubmit)="onSubmit()">
<div class="form-group">
<label >First Name</label>
<input type="text" class="form-control" id="firstName" required [(ngModel)]="client.firstName"
name="firstName">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" id="lastName" required [(ngModel)]="client.lastName"
name="lastName">
</div>
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" id="emailId" required [(ngModel)]="client.emailId"
name="emailId">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
<div [hidden]="!submitted">
<h4>You submitted successfully!</h4>
<!-- <button class="btn btn-success" (click)="newEmployee()">Add</button> -->
</div>
我一直收到这个错误,我不知道为什么,请提供任何帮助
您需要将@Injectable
添加到ClientService
而不是Client
类中:
@Injectable()
export class ClientService {}
否则依赖注入不起作用