【问题标题】:use plugins non ionic-native in Ionic 2在 Ionic 2 中使用非离子原生插件
【发布时间】:2017-07-04 19:23:06
【问题描述】:

我正在使用来自 github 的 cordova 插件,在 ionic native 中不可用。

我有问题。如何在 Ionic 2 中使用非离子原生插件?

我试试:

declare var myPlugin: any;

myPlugin.doSomething(...)

及其他论坛解决方案

但我有错误:

ReferenceError: myPlugin is not defined
at new HomePage (http://localhost:8100/build/main.js:18762:9)

我的配置:

Ionic Framework: ^2.0.0-rc.3
Ionic Native: ^2.2.3
Ionic App Scripts: 1.0.0
Angular Core: 2.2.1
Angular Compiler CLI: 2.2.1
Node: 6.9.5
OS Platform: Windows 10
Navigator Platform: Win32
cordova 6.5.0

同样的问题: Working with non ionic-native plugins in Ionic 2

【问题讨论】:

  • 您能添加一个指向您尝试使用的插件的链接吗?正如您提供的链接中所建议的那样,您是否尝试记录 cordova 以查看插件是否存在?
  • 您是在使用设备还是离子服务?
  • 在离子服务中,测试初始化​​功能。我会在设备上试试。
  • 设备相同“错误:未捕获(在承诺中):ReferenceError:未定义myPlugin ReferenceError:未定义myPlugin”

标签: cordova typescript ionic2 cordova-plugins


【解决方案1】:

请尝试这样:

首先将插件或库导入为:

import plugin from 'plugin-name' ;

然后使用:

plugin.functionName();

【讨论】:

    【解决方案2】:

    您的问题是在 plugin.xml 中 clobbers 设置为 window.plugins.myplugin。 这使得 window 成为全局对象,您必须声明:

    declare var window: any;
    

    和函数被称为:

    window.plugins.myPlugin.pluginInitialize()
    

    或者如果这是您定制的插件,您可以将clobbers 对象设为myPlugin

    【讨论】:

      【解决方案3】:

      有几样东西要看:

      • 验证插件条目是否存在于 plugin.xml 和 package.json 文件中?如果没有,您可能需要添加一个条目,如下例所示。
      • 仅在平台准备就绪时使用插件。
      • 在真实设备中测试您的应用。
      • 使用“ionic add”而不是“cordova add”来添加新插件。

      请参阅以下主题,这可能对编辑您的 plugin.xml 文件有所帮助。

      https://forum.ionicframework.com/t/how-to-use-non-native-plugins-on-ionic-2/63936/17

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-01
        • 1970-01-01
        • 2018-02-23
        • 2019-02-05
        • 1970-01-01
        • 2021-05-09
        • 2021-11-30
        相关资源
        最近更新 更多