简体   繁体   English

如何使用Ionic 2 / TypeScript的(Cordova)插件?

[英]How to use (Cordova) plugin with Ionic 2 / TypeScript?

I would like to use cordova-plugin-fcm with Ionic2/TypeScript. 我想使用带有Ionic2 / TypeScript的cordova-plugin-fcm The wrapper FCMPlugin.js looks real simple but I'm used to Angular2/TypeScript working with import statements and don't know how to get such a plugin to work with Ionic2. 包装器FCMPlugin.js看起来很简单,但我习惯使用Angular2 / TypeScript处理import语句,并且不知道如何使用Ionic2这样的插件。

If I use the code to get a token ( FCMPlugin.getToken() ) I get: 如果我使用代码获取令牌( FCMPlugin.getToken() ),我得到:

Cannot find name 'FCMPlugin' 找不到名字'FCMPlugin'

When I try this suggested answer 当我尝试这个建议的答案

I get: 我明白了:

Require is not defined 要求未定义

Turns out the suggested answer is working after all. 事实证明建议的答案是可行的。 I just made the mistake to test in the browser to see if I could get rid of the error: 我只是错误地在浏览器中测试,看看我是否可以摆脱错误:

Cannot find name 'FCMPlugin'

This then gave me the: 这给了我:

Require is not defined

And I thought I was stuck. 我以为我被卡住了。 Cordova only runs on a simulator or real device but I thought I could do some testing first in the browser until I got rid of the errors (which I could not). Cordova只能在模拟器或真实设备上运行,但我认为我可以先在浏览器中进行一些测试,直到我摆脱错误(我无法做到)。 However when running the code on a simulator or device everything is fine. 但是,当在模拟器或设备上运行代码时,一切都很好。

Important though is that you have to get rid of the TypeScript error "Cannot find name" otherwise it will not run. 但重要的是你必须摆脱TypeScript错误“找不到名字”否则它将无法运行。 To do that without a type definition file (a d.ts file) you can just declare a variable like: 要在没有类型定义文件(d.ts文件)的情况下执行此操作,您只需声明一个变量,如:

declare var FCMPlugin:any;

Just below the imports. 就在进口之下。

Then there will be no TypeScript error and everything runs just fine on a simulator/device. 然后就没有TypeScript错误,一切都在模拟器/设备上运行得很好。

If you want it to work with TypeScript, you can declare in your file as @majodi said: 如果您希望它与TypeScript一起使用,您可以在文件中声明@majodi说:

declare var FCMPlugin: any;

but you will not have auto-complete. 但你不会自动完成。

You can also create a fcm-plugin.d.ts file to put in yourAppName/src/app/fcm-plugin.d.ts and it should look something like this: 您还可以创建一个fcm-plugin.d.ts文件放在yourAppName/src/app/fcm-plugin.d.ts ,它应该如下所示:

declare var fcmPlugin: FCMPlugin.IFCMPlugin;

// Support AMD require
declare module 'fcmPlugin' {
    export = fcmPlugin;
}

declare namespace FCMPlugin {
    interface IFCMPlugin {
        onNotification(onNotificationCallback, successCallback, errorCallback);
        getToken(successCallback, errorCallback);
        onTokenRefresh(onTokenRefreshCallback);
        subscribeToTopic(topic: string, successCallback, errorCallback);
        unsubscribeFromTopic(topic: string, successCallback, errorCallback);
    }
}

Do not forget to call fcmPlugin after check that you're on a cordova device: 在检查您是否在使用cordova设备后,请不要忘记调用fcmPlugin:

if (platform.is('cordova')) {
    fcmPlugin.getToken(token => {
        console.log('getToken() succeed: ', token);
    }, err => {
        console.error('getToken() failed: ', err);
    });
}

Otherwise will have the fcmPlugin does not exists when you do ionic serve 否则fcmPlugin does not exists当你做ionic serve时, fcmPlugin does not existsfcmPlugin does not exists

Then you just have to call fcmPlugin and let the TypeScript auto-completion magic occur! 然后你只需要调用fcmPlugin并让TypeScript自动完成魔术发生!

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

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