在Ionic上添加和使用插件

时间:2023-01-21 16:19:59

Please, I need a final step-by-step to add and use plugins with Ionic. I lost several weeks trying to use plugins on Ionic but everytime I got plugin function "is not defined". I really need to solve this issue, but I don't got success.

请,我需要最后一步一步地添加和使用Ionic插件。我失去了几个星期试图在Ionic上使用插件,但每次我得到插件功能“没有定义”。我真的需要解决这个问题,但我没有成功。

First of all I'm not trying to use native plugins that uses special hardware components like camera. I'm following the steps below:

首先,我不是要使用使用相机等特殊硬件组件的原生插件。我按照以下步骤操作:

1) I created my app using cordova

1)我使用cordova创建了我的应用程序

2) I added my plugin using:

2)我添加了我的插件:

cordova plugin add mercadopago-plugin

cordova插件添加mercadopago-plugin

3) I've inserted this following scripts on my index.html:

3)我在index.html上插入了以下脚本:

<script src="cordova.js"></script>
<script src="app.js"></script>

4) I created a button calling my plugin on my template.html

4)我在template.html上创建了一个调用我的插件的按钮

<button ng-click="startCheckout()"> OK </button>

5) I called my plugin on app.js

5)我在app.js上调用了我的插件

.controller('MyCtrl', function($scope) {    
        $scope.carrinho = allcarrinho;      
        var publicKey = "TEST";
        $scope.startCheckout = function(){
                MercadoPago.startCheckout(publicKey, prefId, null, false, success, failure);
        }               
})  

6) I've emulated my app on the browser typing on my admin prompt command:

6)我在我的管理员提示命令上输入的浏览器上模拟了我的应用程序:

ionic serve

But when the plugin is called I got this error:

但是当调用插件时我得到了这个错误:

ReferenceError: MercadoPago is not defined

ReferenceError:未定义MercadoPago

I following everything on documentation:

我关注文档的所有内容:

Plugin's documentation: http://mercadopago.github.io/px-hybrid/

插件的文档:http://mercadopago.github.io/px-hybrid/

How do I to fix it?

我该如何解决?

Thanks!

谢谢!

3 个解决方案

#1


2  

You probably have to inject MercadoPago in your controller:

您可能必须在控制器中注入MercadoPago:

.controller('MyCtrl', function($scope, MercadoPago /*<--here*/) {  

I didn't see your full code but it has to be something like that.

我没有看到你的完整代码,但它必须是这样的。

or to have it not break when minifying:

或者在缩小时不要破坏:

.controller('MyCtrl', ['$scope', 'MercadoPago', function($scope, MercadoPago) { ....

See this

看到这个

#2


1  

Most Cordova plugins do not work in the browser. You should try on an emulator or simulator.

大多数Cordova插件在浏览器中不起作用。你应该尝试模拟器或模拟器。

Either that, or you need to wait for document.ready or ionicPlatform.ready for the plugin to initialize before trying to use it

或者,或者您需要等待document.ready或ionicPlatform.ready以便在尝试使用它之前初始化插件

#3


1  

This plugin makes native calls when you use ˝MercadoPago". It won't work in your browser, you should run it on an Android Emulator or Phone.

当您使用“˝MercadoPago”时,此插件会进行本机调用。它不能在您的浏览器中运行,您应该在Android模拟器或手机上运行它。

Try:

尝试:

ionic emulate android -l -c

And it should work.

它应该工作。

#1


2  

You probably have to inject MercadoPago in your controller:

您可能必须在控制器中注入MercadoPago:

.controller('MyCtrl', function($scope, MercadoPago /*<--here*/) {  

I didn't see your full code but it has to be something like that.

我没有看到你的完整代码,但它必须是这样的。

or to have it not break when minifying:

或者在缩小时不要破坏:

.controller('MyCtrl', ['$scope', 'MercadoPago', function($scope, MercadoPago) { ....

See this

看到这个

#2


1  

Most Cordova plugins do not work in the browser. You should try on an emulator or simulator.

大多数Cordova插件在浏览器中不起作用。你应该尝试模拟器或模拟器。

Either that, or you need to wait for document.ready or ionicPlatform.ready for the plugin to initialize before trying to use it

或者,或者您需要等待document.ready或ionicPlatform.ready以便在尝试使用它之前初始化插件

#3


1  

This plugin makes native calls when you use ˝MercadoPago". It won't work in your browser, you should run it on an Android Emulator or Phone.

当您使用“˝MercadoPago”时,此插件会进行本机调用。它不能在您的浏览器中运行,您应该在Android模拟器或手机上运行它。

Try:

尝试:

ionic emulate android -l -c

And it should work.

它应该工作。