我正在使用Angular 5 Service
调用POST API服务文件
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse, HttpClientModule } from '@angular/common/http';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { HttpResponse } from 'selenium-webdriver/http';
import { IScheduleAPI } from '../interfaces/ischeduleAPI';
// import { Ischedule } from '../interfaces/ischedule';
@Injectable()
export class SchedulesService {
apiURL = 'http://localhost:57863/api/Audit/';
ScheduleDetail: IScheduleAPI[] = [];
constructor(private http: Http) { }
GetScheduleAPI(params, httpOptions) {
return this.http.post<IScheduleAPI[]>(this.apiURL, params, httpOptions)
.catch(this.errorHandler);
}
errorHandler(error: HttpErrorResponse) {
return Observable.throw(error.message || 'Server Error');
}
}
我在我的组件中调用此服务
import { Component, OnInit } from '@angular/core';
import { HttpClientModule, HttpHeaders } from '@angular/common/http';
import { IScheduleAPI } from '../interfaces/ischeduleAPI';
import { SchedulesService } from '../services/schedules.service';
import { Http, Response } from '@angular/http';
@Component({
selector: 'app-schedules',
templateUrl: './schedules.component.html',
styleUrls: ['./schedules.component.css']
})
export class SchedulesComponent implements OnInit {
Schedule: IScheduleAPI[];
message: string;
constructor(private sch: SchedulesService, private http: Http) { }
ngOnInit() {
const params = [{
'ActionMethod': 'GetSchedule',
'StaffCode': 'NA',
'Password': 'NA'
}];
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
this.sch.GetScheduleAPI(params, httpOptions).subscribe(data => this.Schedule = data,
error => this.message = error);
}
}
现在出现问题
1。] 它显示错误,即预期0类型参数但在行中得到1
return this.http.post<IScheduleAPI[]>(this.apiURL, params, httpOptions)
它的内部服务文件,这是我在其他服务文件中使用的标记,它正常工作。
删除IScheduleAPI []后,它停止显示错误,但没有点击API,而是显示错误
我正在使用Angular 5
答案 0 :(得分:1)
您似乎在任何地方使用新的HttpClient
,但在构造函数中。
您需要做的就是替换
constructor(private http: Http) { }
与
constructor(private http: HttpClient) { }
SchedulesService
和SchedulesComponent