reflective_provider.js:Uncaught无法解析Service的所有参数

时间:2016-07-09 13:42:10

标签: angular

错误

  

reflective_provider.js:240 Uncaught无法解析所有参数   ' NavService'(?)。确保所有参数都用   注入或拥有有效的类型注释以及“NavService”#39;是   用Injectable装饰。

NavService

import {Injector, Injectable} from '@angular/core'
import {NavParams, NavController, ViewController} from 'ionic-angular'

import {IdentityPage} from '../../../pages/transaction/identity/identity';
import {AddressPage} from '../../../pages/transaction/address/address';
import {PersonalInfoPage} from '../../../pages/transaction/personal-info/personal-info';
import {PaymentDetailsPage} from '../../../pages/transaction/payment-details/payment-details';

@Injectable()

export class NavService {

    private identityPage = IdentityPage
    private personalInfoPage = PersonalInfoPage
    private paymentDetailsPage = PaymentDetailsPage
    private addressPage = AddressPage

    constructor (
        private _pagesToLoad = []
    ) {}

    public lookAround(currentPage){

        let prvIndex = this._pagesToLoad.indexOf(currentPage)-1
        let nxtIndex = this._pagesToLoad.indexOf(currentPage)+1

        let neighboursTuple = {
            prvIndex: this._pagesToLoad[prvIndex],
            nxtIndex: this._pagesToLoad[nxtIndex]
        }
        return neighboursTuple
    }
}

使用NavService的组件

import {NavService} from '../../../providers/services/nav-service/nav-service';

import {OnInit, AfterViewInit, ViewChildren, ContentChild} from '@angular/core'
import {TranslateService, TranslatePipe} from 'ng2-translate/ng2-translate'
import {NavParams, NavController, ViewController, Events, MenuController, Nav, Slides} from 'ionic-angular'

import {ControlGroup, FormBuilder, Validators} from '@angular/common'
import {Component, ViewChild, Directive, ElementRef, Input, DynamicComponentLoader, Injector,provide} from '@angular/core'


@Component({
    templateUrl:    'build/pages/transaction/personal-info/personal-info.html'
    providers:      [NavService]
})

export class PersonalInfoPage {

    infoForm: ControlGroup

    constructor (
        public formBuilder: FormBuilder, 
        private authService: AuthService,
        private navService: NavService,
        private navController: NavController,
        private viewController: ViewController
    ) { ...

我在这里看不到什么错?

更新 - 测试

 describe('NavService', () => {

        it ('should have instantiated nav service with List of Pages and receive neighbours', () => {

            let navService = new NavService([PersonalInfoPage,AddressPage,IdentityPage,PaymentDetailsPage])
            let ResultNeighboursTuple = navService.lookAround(AddressPage)

            expect(ResultNeighboursTuple.prvIndex).toEqual((PersonalInfoPage))
            expect(ResultNeighboursTuple.nxtIndex).toEqual((IdentityPage))
         })

2 个答案:

答案 0 :(得分:1)

export class NavService {

    private identityPage = IdentityPage
    private personalInfoPage = PersonalInfoPage
    private paymentDetailsPage = PaymentDetailsPage
    private addressPage = AddressPage;
private _pagesToLoad = [];

    constructor (
        // private _pagesToLoad = []  <---- put it out side of constructor
    ) {}

    public lookAround(currentPage){

        let prvIndex = this._pagesToLoad.indexOf(currentPage)-1
        let nxtIndex = this._pagesToLoad.indexOf(currentPage)+1

        let neighboursTuple = {
            prvIndex: this._pagesToLoad[prvIndex],
            nxtIndex: this._pagesToLoad[nxtIndex]
        }
        return neighboursTuple
    }
}

答案 1 :(得分:0)

原来我的测试导致错误,只是确保你在注射前提供测试,如下所示:

describe('Service: LanguagesService', () => {
  let service;

  //setup
  beforeEachProviders(() => [
    LanguagesService
  ]);

  beforeEach(inject([LanguagesService], s => {
    service = s;
  }));

  //specs
  it('should return available languages', () => {
    let languages = service.get();
    expect(languages).toContain('en');
    expect(languages).toContain('es');
    expect(languages).toContain('fr');
    expect(languages.length).toEqual(3);
  });
}) 

https://gist.github.com/gsans/cd72e5b0f5302bc9dbc8#file-languagesservice-spec-js