Ionic2:没有提供商错误

时间:2017-03-22 11:09:45

标签: ionic2

我正在使用Ionic2 rc4

Your system information:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.4
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
Ionic App Scripts Version: 1.0.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: macOS Sierra
Node Version: v6.9.4
Xcode version: Xcode 8.2.1 Build version 8C1002

我想添加一项服务。我有其他Services,完美无缺。所以我将这个新服务(PayPalService)配置为相同。

payPalTest.ts

import { Component, Inject, forwardRef } from '@angular/core';
import { PayPalService } from '../paypal/PayPalService';

@Component({
  templateUrl: 'payPalTest.html'
})
export class PayPalTestPage {
  public payPalService: PayPalService = null;

  constructor( @Inject(forwardRef(() => PayPalService)) payPalService) {
    this.payPalService = payPalService;
  }

  public payOut(): void {
    alert('payOut');
    //this.payPalService.payOut();
  }

}

payPalService.ts

declare var require: any;
var paypal = require('paypal-rest-sdk');
//import {paypal-rest-sdk} from './paypal-rest-sdk';
import { Injectable } from "@angular/core";

@Injectable()
export class PayPalService {

    public paypal: any = null;

    constructor() {
    }
}

app.module.ts

import { PayPalService } from "../pages/paypal/payPalService";
import { PayPalTestPage } from "../pages/paypal/payPalTest";
...
@NgModule({
  declarations: [
      ...
    PayPalTestPage
      ...
  entryComponents: [
      ...
    PayPalTestPage
      ...
  ...
  providers: [..., PayPalService]

但是,我收到以下错误。

错误

  ./MyApp类MyApp中的

运行时错误错误 - 由以下原因引起:无提供程序   对于PayPalService!

我认为错误与我导入'paypal-rest-sdk'的方式有关。

declare var require: any;
var paypal = require('paypal-rest-sdk');

我按如下方式安装了paypal-rest-sdk

npm install paypal-rest-sdk

新包已添加到node_modules

问题

有人可以建议我如何解决上述错误吗?

更新

如果我通过注释掉以下两行来删除对PayPal api的所有引用:

payPalService.ts

// declare var require: any;
// var paypal = require('paypal-rest-sdk');

我收到以下错误:

错误

  

运行时错误模块构建失败:错误:ENOENT:没有这样的文件或   目录,打开   ' /Users/richardmarais/Development/ionic/theWhoZoo/src/pages/paypal/payPalService.js'   在错误(本机)

1 个答案:

答案 0 :(得分:0)

这是一个非常愚蠢的错误。导入的情况不正确。

变化:

<强> payPalTest.ts

import { PayPalService } from '../paypal/PayPalService';

为:

import { PayPalService } from '../paypal/payPalService';