依赖注入导致构建打破
问题描述:
我正在一个项目中,我需要在服务层上使用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注释。
不应使用@Injectable注释HomeService吗? – toskv
@toskv,谢谢你的建议,你是对的!这正是问题所在。 – AnchovyLegend
我也做了一个答案,如果它为你修好了,请把它标记为正确的。 :) – toskv