Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic2 离子2:如何在控制器类中调用提供程序函数_Ionic2_Observable_Angular2 Providers - Fatal编程技术网

Ionic2 离子2:如何在控制器类中调用提供程序函数

Ionic2 离子2:如何在控制器类中调用提供程序函数,ionic2,observable,angular2-providers,Ionic2,Observable,Angular2 Providers,我只在我的机器上创建了新的Cordova插件。然后我把它添加到我的项目中。当我调用该插件时,它工作正常。现在,我尝试为我的插件创建一个结构化调用程序。我为它创建了一个提供程序,但问题是我不知道如何从控制器类调用插件函数。下面是我的示例代码 提供商:我的服务.ts import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; dec

我只在我的机器上创建了新的Cordova插件。然后我把它添加到我的项目中。当我调用该插件时,它工作正常。现在,我尝试为我的插件创建一个结构化调用程序。我为它创建了一个提供程序,但问题是我不知道如何从控制器类调用插件函数。下面是我的示例代码

提供商:我的服务.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

declare let myPlugin: any;

@Injectable()
export class MyService {

  constructor(public http: Http) {
    console.log('Hello MyService Provider');
  }

  public myFunction() {
    myPlugin.myPluginFunction(
      (data) => {
        return data;
      },

      (err) => {
        return err;
      });
  }
}
import { Component } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';

import { MyService } from '../../providers/my-service';

@Component({
  selector: 'page-my-page-ionic',
  templateUrl: 'hello-ionic.html'
})
export class MyPage {
  constructor(private viewCtrl: ViewController, private myService: MyService) {}

  ionViewWillEnter() {

    //I tried to call like this
    this.myService.myFunction().subscribe(
      data => {
        alert("success");
      },
      error => {
        alert("error");
      });
  }
}
页面:我的页面.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

declare let myPlugin: any;

@Injectable()
export class MyService {

  constructor(public http: Http) {
    console.log('Hello MyService Provider');
  }

  public myFunction() {
    myPlugin.myPluginFunction(
      (data) => {
        return data;
      },

      (err) => {
        return err;
      });
  }
}
import { Component } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';

import { MyService } from '../../providers/my-service';

@Component({
  selector: 'page-my-page-ionic',
  templateUrl: 'hello-ionic.html'
})
export class MyPage {
  constructor(private viewCtrl: ViewController, private myService: MyService) {}

  ionViewWillEnter() {

    //I tried to call like this
    this.myService.myFunction().subscribe(
      data => {
        alert("success");
      },
      error => {
        alert("error");
      });
  }
}

它返回我这个错误-
属性“subscribe”在类型“void”上不存在。
我不知道如何调用该函数,因为我的提供者返回我
success
error

我想既然你的
myFunction()
没有返回任何可观察到的内容,你就不能订阅它。它只是直接返回数据

在这种情况下,您可以这样使用它:

var data = this.myService.myFunction();
console.log("Data from plugin is :", data);
如果要将其用作可观察对象,请返回一个新的可观察对象,如下所示:

public myFunction() {
    return Observable.create(observer => {
        myPlugin.myPluginFunction(
        (data) => {
            observer.next(data);
        },
        (err) => {
            observer.next(data);
        });
    },
    (err) => {
        observer.error(err);
    });
}

我认为,由于您的
myFunction()
没有返回任何可观察到的内容,因此您无法订阅它。它只是直接返回数据

在这种情况下,您可以这样使用它:

var data = this.myService.myFunction();
console.log("Data from plugin is :", data);
如果要将其用作可观察对象,请返回一个新的可观察对象,如下所示:

public myFunction() {
    return Observable.create(observer => {
        myPlugin.myPluginFunction(
        (data) => {
            observer.next(data);
        },
        (err) => {
            observer.next(data);
        });
    },
    (err) => {
        observer.error(err);
    });
}
谢谢你,它的工作:)。现在我知道如何使用Observable了。谢谢你,它可以工作:)。所以现在我知道如何使用可观察的。