离子2供应商

时间:2016-12-09 09:09:32

标签: ionic2

我正在使用this guide将我的应用从Ionic 2.0.0-beta.20升级到Ionic 2.0.0-rc.3

关于名为provider的{​​{1}}我遇到了一个问题,我有点困惑,如果有人可以提供帮助,我会很感激。

问题

我在RatingService中收到以下错误。

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'. 使用provide:useClass:,但我不确定这些值应该是什么?

代码

app.module.ts

provider

ratingService.ts

  import { RatingService } from '../pages/service/ratingService';
    ...
  providers: [{ provide: ErrorHandler, 
    useClass: IonicErrorHandler, 
    RatingService,
    JobService, 
    UtilityService, 
    ...

parentService.ts

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;
    }
}

2 个答案:

答案 0 :(得分:4)

应该是:

providers: [{ provide: ErrorHandler, 
useClass: IonicErrorHandler}, 
RatingService,
JobService, 
UtilityService,

答案 1 :(得分:0)

如果您不小心使用

,也会出现此错误
import RatingService from '../pages/service/ratingService';  

而不是

import { RatingService } from '../pages/service/ratingService';

这个拼写错误花了我2个小时,所以我添加了这个答案,希望能为其他人带来同样的挫败感。