Ionic2 离子2提供者
我正在使用将我的应用程序从Ionic2 离子2提供者,ionic2,Ionic2,我正在使用将我的应用程序从Ionic 2.0.0-beta.20升级到Ionic 2.0.0-rc.3 我有一个关于提供商的问题,称为评级服务,对此我有点困惑,如果有人能提供帮助,我将不胜感激 问题 我在app.module.ts中遇到以下错误 [ts] Argument of type '{ declarations: any[]; imports: ModuleWithProviders[]; bootstrap: typeof IonicApp[]; entryCompone...' is
Ionic 2.0.0-beta.20升级到Ionic 2.0.0-rc.3
我有一个关于提供商
的问题,称为评级服务
,对此我有点困惑,如果有人能提供帮助,我将不胜感激
问题
我在app.module.ts
中遇到以下错误
[ts] Argument of type '{ declarations: any[]; imports: ModuleWithProviders[]; bootstrap: typeof IonicApp[]; entryCompone...' is not assignable to parameter of type 'NgModule'.
Types of property 'providers' are incompatible.
Type '{ provide: typeof ErrorHandler; useClass: typeof IonicErrorHandler; RatingService: typeof RatingS...' is not assignable to type 'Provider[]'.
Type '{ provide: typeof ErrorHandler; useClass: typeof IonicErrorHandler; RatingService: typeof RatingS...' is not assignable to type 'Provider'.
Object literal may only specify known properties, and 'RatingService' does not exist in type 'Provider'.
我认为我需要为每个提供者使用提供:
和使用类:
,但我不确定这些值应该是什么
代码
应用程序模块.ts
import { RatingService } from '../pages/service/ratingService';
...
providers: [{ provide: ErrorHandler,
useClass: IonicErrorHandler,
RatingService,
JobService,
UtilityService,
...
import { Injectable, Inject } from "@angular/core";
import { RatingModel } from '../model/RatingModel';
import { NavController } from 'ionic-angular';
import { Http, Headers } from "@angular/http"
import { ParentService } from "../service/parentService";
import 'rxjs/add/operator/map';
@Injectable()
export class RatingService extends ParentService {
public BASE_URI: String = super.getBaseUrl()+'/rating';
public http: Http = null;
constructor( @Inject(Http) http: Http) {
super();
this.http = http;
}
getRatingRange(firstResult: number, maxResults: number): Promise<RatingModel[]> {
return new Promise<RatingModel[]>(resolve => {
this.getRatingRangeHttpCall(firstResult, maxResults).subscribe(
data => {
var ratingModels: RatingModel[] = [];
for (var index = 0; index < data.length; index++) {
var element = data[index];
ratingModels.push(element);
}
resolve(ratingModels);
},
error => alert('RatingRangeDataPromise(' + firstResult + ', ' + maxResults + ') Data not available. Please try again.\n' + error),
() => {
});
});
}
getRatingForJobRange(firstResult: number, maxResults: number, jobId: number): Promise<RatingModel[]> {
return new Promise<RatingModel[]>(resolve => {
this.getRatingRangeForJobHttpCall(firstResult, maxResults, jobId).subscribe(
data => {
var ratingModels: RatingModel[] = [];
for (var index = 0; index < data.length; index++) {
var element = data[index];
ratingModels.push(element);
}
resolve(ratingModels);
},
error => alert('RatingForJobRange(' + firstResult + ', ' + maxResults + ', ' + jobId + ') Data not available. Please try again.\n' + error),
() => {
});
});
}
saveRating(ratingModel: RatingModel): Promise<RatingModel> {
return new Promise<RatingModel>(resolve => {
this.saveRatingHttpCall(ratingModel).subscribe(
data => {
resolve(data);
},
error => alert('Save Unsuccesfull.\n' + error),
() => {
});
});
}
getRating(id: number): Promise<RatingModel> {
return new Promise<RatingModel>(resolve => {
this.getRatingHttpCall(id).subscribe(
data => {
resolve(data);
},
error => alert('getRating Data not available. Please try again.\n' + error),
() => {
//console.log("Finished getRating");
});
});
}
public getRatingRangeHttpCall(firstResult: number, maxResults: number) {
return this.http.get(this.BASE_URI + '/list/range/' + firstResult + '/' + maxResults)
.map(res => res.json());
}
public getRatingRangeForJobHttpCall(firstResult: number, maxResults: number, jobId: number) {
return this.http.get(this.BASE_URI + '/list/range/' + firstResult + '/' + maxResults + '/' + jobId)
.map(res => res.json());
}
public saveRatingHttpCall(ratingModel: RatingModel) {
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
return this.http.post(this.BASE_URI + '/save', ratingModel, {
headers: headers
})
.map(res => res.json());
}
public getRatingHttpCall(id: number) {
return this.http.get(this.BASE_URI + '/list/' + id)
.map(res => res.json());
}
}
import { Injectable, Inject } from "@angular/core";
@Injectable()
export class ParentService {
public PARENT_BASE_URI: string = 'http://localhost:8080/jbosswildfly';
//public PARENT_BASE_URI: string = 'http://jbosswildfly-easypeasy.rhcloud.com';
constructor() {
}
public getBaseUrl(): string {
return this.PARENT_BASE_URI;
}
}
评级服务.ts
import { RatingService } from '../pages/service/ratingService';
...
providers: [{ provide: ErrorHandler,
useClass: IonicErrorHandler,
RatingService,
JobService,
UtilityService,
...
import { Injectable, Inject } from "@angular/core";
import { RatingModel } from '../model/RatingModel';
import { NavController } from 'ionic-angular';
import { Http, Headers } from "@angular/http"
import { ParentService } from "../service/parentService";
import 'rxjs/add/operator/map';
@Injectable()
export class RatingService extends ParentService {
public BASE_URI: String = super.getBaseUrl()+'/rating';
public http: Http = null;
constructor( @Inject(Http) http: Http) {
super();
this.http = http;
}
getRatingRange(firstResult: number, maxResults: number): Promise<RatingModel[]> {
return new Promise<RatingModel[]>(resolve => {
this.getRatingRangeHttpCall(firstResult, maxResults).subscribe(
data => {
var ratingModels: RatingModel[] = [];
for (var index = 0; index < data.length; index++) {
var element = data[index];
ratingModels.push(element);
}
resolve(ratingModels);
},
error => alert('RatingRangeDataPromise(' + firstResult + ', ' + maxResults + ') Data not available. Please try again.\n' + error),
() => {
});
});
}
getRatingForJobRange(firstResult: number, maxResults: number, jobId: number): Promise<RatingModel[]> {
return new Promise<RatingModel[]>(resolve => {
this.getRatingRangeForJobHttpCall(firstResult, maxResults, jobId).subscribe(
data => {
var ratingModels: RatingModel[] = [];
for (var index = 0; index < data.length; index++) {
var element = data[index];
ratingModels.push(element);
}
resolve(ratingModels);
},
error => alert('RatingForJobRange(' + firstResult + ', ' + maxResults + ', ' + jobId + ') Data not available. Please try again.\n' + error),
() => {
});
});
}
saveRating(ratingModel: RatingModel): Promise<RatingModel> {
return new Promise<RatingModel>(resolve => {
this.saveRatingHttpCall(ratingModel).subscribe(
data => {
resolve(data);
},
error => alert('Save Unsuccesfull.\n' + error),
() => {
});
});
}
getRating(id: number): Promise<RatingModel> {
return new Promise<RatingModel>(resolve => {
this.getRatingHttpCall(id).subscribe(
data => {
resolve(data);
},
error => alert('getRating Data not available. Please try again.\n' + error),
() => {
//console.log("Finished getRating");
});
});
}
public getRatingRangeHttpCall(firstResult: number, maxResults: number) {
return this.http.get(this.BASE_URI + '/list/range/' + firstResult + '/' + maxResults)
.map(res => res.json());
}
public getRatingRangeForJobHttpCall(firstResult: number, maxResults: number, jobId: number) {
return this.http.get(this.BASE_URI + '/list/range/' + firstResult + '/' + maxResults + '/' + jobId)
.map(res => res.json());
}
public saveRatingHttpCall(ratingModel: RatingModel) {
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
return this.http.post(this.BASE_URI + '/save', ratingModel, {
headers: headers
})
.map(res => res.json());
}
public getRatingHttpCall(id: number) {
return this.http.get(this.BASE_URI + '/list/' + id)
.map(res => res.json());
}
}
import { Injectable, Inject } from "@angular/core";
@Injectable()
export class ParentService {
public PARENT_BASE_URI: string = 'http://localhost:8080/jbosswildfly';
//public PARENT_BASE_URI: string = 'http://jbosswildfly-easypeasy.rhcloud.com';
constructor() {
}
public getBaseUrl(): string {
return this.PARENT_BASE_URI;
}
}
更新
我已将代码更改为以下内容,我将对其进行测试,看看它是否有效:
providers: [
{ provide: ErrorHandler, useClass: IonicErrorHandler},
{ provide: RatingService, useClass: RatingService},
{ provide: JobService, useClass: JobService},
{ provide: UtilityService, useClass: UtilityService}
]
应该是:
providers: [{ provide: ErrorHandler,
useClass: IonicErrorHandler},
RatingService,
JobService,
UtilityService,
应该是:
providers: [{ provide: ErrorHandler,
useClass: IonicErrorHandler},
RatingService,
JobService,
UtilityService,
如果意外使用,也会出现此错误
import RatingService from '../pages/service/ratingService';
而不是
import { RatingService } from '../pages/service/ratingService';
这个输入错误花费了我2个小时,所以我添加这个答案,希望它能让其他人免于同样的挫折。如果您不小心使用
import RatingService from '../pages/service/ratingService';
而不是
import { RatingService } from '../pages/service/ratingService';
那个打字错误花了我2个小时,所以我添加这个答案,希望它能让其他人免于同样的挫折。是否应该用}
关闭{provide:…useClass..
呢?(不确定,我自己没有这样使用过它){code>{provide:…useClass..
是否应该用一个}
来关闭?(不确定,我自己没有这样使用过它。)