简体   繁体   English

从ionic2中的javascript访问打字稿方法

[英]Access typescript methods from javascript in ionic2

I'm tyring to call a typescript method from javascript in ionic2. 我想从ionic2中的javascript调用打字稿方法。 Let's consider a ionic2 page generated by "ionic g page MyPage" where I have created a method myNewMethod". 让我们考虑一个由“ ionic g page MyPage”生成的ionic2页面,其中我创建了一个方法“ myNewMethod”。

Is there any way to call myNewMethod" from a javascript function which is part of a cordova plugin? I've noticed that in main.js, as transpiled in javascripte, there is a variable MyPage and myNewMethod is defined as prototype. However If I call MyPage.myNewMethod doesn't seem to work. 有什么方法可以从Cordova插件中的javascript函数中调用“ myNewMethod”吗?我注意到在main.js中,如在javascripte中编译的那样,有一个变量MyPage并且myNewMethod被定义为原型。但是如果我调用MyPage.myNewMethod似乎不起作用。

I have further noticed the following part of code: 我进一步注意到以下代码部分:

/* 312 */
(function(module, __webpack_exports__, __webpack_require__) {

"use strict";
var __WEBPACK_IMPORTED_MODULE_0__angular_core__ = __webpack_require__(0);
var __WEBPACK_IMPORTED_MODULE_1_ionic_angular__ = __webpack_require__(136);
__webpack_require__.d(__webpack_exports__, "a", function() { return MyPage; });
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};


var MyPage = (function () {
    function MyPage(navCtrl, navParams) {
        this.navCtrl = navCtrl;
        this.navParams = navParams;
    }
    MyPage.prototype.ionViewDidLoad = function () {
        console.log('ionViewDidLoad MyPage');
    };
    MyPage.prototype.myNewMethod = function () {
        //do stuff
    };
    MyPage = __decorate([
        __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__angular_core__["B"])({
            selector: 'page-add-item',template:'Some Template'
        }),
        __metadata('design:paramtypes', [__WEBPACK_IMPORTED_MODULE_1_ionic_angular__["d"], __WEBPACK_IMPORTED_MODULE_1_ionic_angular__["e"]])
    ], MyPage);
    return MyPage;
}());


}),

It seems that the scope of MyPage is not available directly and it's being returned through webpack_requare 似乎MyPage的范围不能直接使用,而是通过webpack_requare返回的

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

Probably it's not the best way since I make MyPage public, but at least it works. 自从我公开MyPage以来,这可能不是最好的方法,但至少它能起作用。 What I did is I assigned MyPage to window variable, ie during ionViewDidLoad I wrote: 我所做的是将MyPage分配给窗口变量,即在ionViewDidLoad期间我写了:

eval('window.MyPage = this');

MyMethod is considered a prototype and as a consequence I can call it from javascript code through: MyMethod被认为是原型,因此,我可以通过以下方式从javascript代码中调用它:

window.MyPage.MyMethod();

Another way which is more correct is to override the function that exists in the cordova plugin. 另一种更正确的方法是重写cordova插件中存在的功能。 If we consider 如果我们考虑

MyCordovaPlugin.prototype.aMethodToOveride=function(){}

Then form MyPage in the constructor 然后在构造函数中形成MyPage

var MyPage = this; 
cordova.plugins.MyCordovaPlugin.aMethodToOveride=function(){ 
    MyPage.MyMethod();  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM