在Ionic 2中集成JavaScript Web插件

时间:2017-12-07 08:35:02

标签: javascript jquery ionic2 applozic

我试图将Applozic聊天平台整合到我的Ionic 2项目中,我想将其导出到Web,Android和iOS。使用sample作为基础并为Javascript集成过程创建applozic.d.ts & applozichv.js

applozic.d.ts

interface AppLozicStatic {
    initPlugin(): any;
}

declare var AppLozic : AppLozicStatic;
export = AppLozic;

applozichv.js

(function () {
    var root = this;

    var AppLozic = function (obj) {
        if (obj instanceof AppLozic) return obj;
        if (!(this instanceof AppLozic)) return new AppLozic(obj);
        // this.EXIFwrapped = obj;
    };

    if (typeof exports !== 'undefined') {
        if (typeof module !== 'undefined' && module.exports) {
            exports = module.exports = AppLozic;
        }
        exports.AppLozic = AppLozic;
    } else {
        root.AppLozic = AppLozic;
    }

    AppLozic.initPlugin = function () {
        var $original;
        // var $applozic = "";
        var $applozic;
        console.log("initPlugin");

        $original = jQuery.noConflict(true);
        $ = $original;
        jQuery = $original;


        if (typeof jQuery !== 'undefined') {
            console.log("initPlugin 1");


            $applozic = jQuery.noConflict(true);

            $applozic.fn
                .applozic({

                    baseUrl : 'https://apps.applozic.com',
                                    userId : 'debug454545', //TODO: replace userId with actual UserId
                                    userName : 'test',          //TODO: replace userId with actual UserName
                                        appId : 'applozic-sample-app',
//                        accessToken: 'suraj',                             //TODO: set user access token.for new user it will create new access token

                    ojq: $original,
                    // obsm: oModal,

                    //optional, leave it blank for testing purpose, read this if you want to add additional security by verifying password from your server https://www.applozic.com/docs/configuration.html#access-token-url
                    //  authenticationTypeId: 1,    //1 for password verification from Applozic server and 0 for access Token verification from your server
                    //  autoTypeSearchEnabled : false,
                    //  messageBubbleAvator: true,
                    notificationIconLink: "https://www.applozic.com/resources/images/applozic_icon.png",
                    notificationSoundLink: "",
                    readConversation: readMessage, // readMessage function defined above
                    onInit: onInitialize, //callback function execute on plugin initialize
                    maxAttachmentSize: 25, //max attachment size in MB
                    desktopNotification: true,
                    locShare: true,
                    video: true,
                    topicBox: true,
//                        mapStaticAPIkey: "AIzaSyCWRScTDtbt8tlXDr6hiceCsU83aS2UuZw",
//                        googleApiKey: "AIzaSyDKfWHzu9X7Z2hByeW4RRFJrD9SizOzZt4" // replace it with your Google API key
                    // initAutoSuggestions : initAutoSuggestions //  function to enable auto suggestions
                });
        }
        var oModal = "";

        /*if (typeof $original !== 'undefined') {
            $ = $original;
            jQuery = $original;
            if (typeof $.fn.modal === 'function') {
                oModal = $.fn.modal.noConflict();
            }
        } else {
            $ = $applozic;
            jQuery = $applozic;
            if (typeof $applozic.fn.modal === 'function') {
                oModal = $applozic.fn.modal.noConflict();
            }
        }*/

        //Sample json contains display name and photoLink for userId


        function readMessage() {
            //console.log(userId);
        }


        //callback function execute after plugin initialize.
        function onInitialize(response, data) {
            if (response.status === 'success') {
                // $applozic.fn.applozic('loadContacts', {'contacts':contactsJSON});
                // $applozic.fn.applozic('loadTab', 'shanki.connect');
                //write your logic exectute after plugin initialize.
                alert("success");
            } else {
                alert(response.errorMessage);
            }
        }


        // init();

    };


})();

我将以上创建的所有文件(包括applozic.common.jsapplozic.fullview.jsjquery.min.js)添加到assets/js文件夹,并将其链接到我的index.html。这是我从applozic.js执行chat.ts中的JavaScript方法的唯一方法。

我现在遇到的问题是我收到了错误:

TypeError: Cannot read property 'noConflict' of undefined` in `applozic.js`

就行了

$original = jQuery.noConflict(true);

因此if块的其余部分也没有执行。

为了让jQuery在项目中工作,我尝试通过执行以下命令通过NPM安装它:

npm install jquery --save
npm install @types/jquery --save

但是这导致了ionic serve中出现以下错误的问题:

JavaScript heap out of memory

我真的需要帮助来执行我的applozic.js文件来初始化和调用聊天插件函数。

1 个答案:

答案 0 :(得分:0)

您应该首先确定实际问题。

如果jquery安装存在问题,则可以使用以下行对其进行调试。

console.log("Jwuery Text : " , $('.testClassName'));

如果在控制台中完全打印了以下log,则jquery中没有错误。

使用reInitialize内存堆可以通过以下命令解决此内存大小问题:

set NODE_OPTIONS=--max_old_space_size=4096
or
node --max_old_space_size=4096

此命令运行到命令提示符后,尝试服务项目或构建项目。