0
我正在一個項目中,我需要在服務層上使用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>
不應使用@Injectable註釋HomeService嗎? – toskv
@toskv,謝謝你的建議,你是對的!這正是問題所在。 – AnchovyLegend
我也做了一個答案,如果它爲你修好了,請把它標記爲正確的。 :) – toskv