我正在开发一个项目,我需要在服务层上使用Http。下面我试图像往常一样将Http注入到构造函数中,但是当我尝试ionic build ios
时,我收到以下错误:
[08:54:51] Error: Error at /Users/zzz/Sites/angular-app/Test-Wondersign/.tmp/pages/home/home.ngfactory.ts:64:29
[08:54:51] Supplied parameters do not match any signature of call target.
[08:54:51] ngc failed
[08:54:51] ionic-app-script task: "build"
[08:54:51] Error: Error
home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HomeService } from '../home/home.service';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [HomeService]
})
export class HomePage {
users: any;
constructor(public navCtrl: NavController, public hs: HomeService) {
this.loadUsers();
}
loadUsers() {
this.hs.getUsers().then( (response) => {
this.users = JSON.parse(response._body);
});
}
}
home.service.ts
import { Http,Headers, RequestOptions } from '@angular/http';
import {Inject} from '@angular/core';
export class HomeService {
url:string;
constructor(@Inject(Http) public http: Http) {
this.url ='https://jsonplaceholder.typicode.com/users';
}
getUsers(): Promise<any> {
return new Promise((resolve, reject) => {
this.http.get(this.url).subscribe( result => {
resolve(result);
});
});
}
}
home.html的
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<div *ngFor="let user of users">
{{ user.name}} {{ user.email}}
</div>
</ion-content>
答案 0 :(得分:1)
您的 HomeService 必须使用@Injectable进行注释。