在Ionic 2中使用TypeScript使用第三方cordova插件(Using a third-party cordova plugin in Ionic 2 with TypeScript)

问 题

In my Ionic 2 app (TypeScript), where I use plugins, for example the Camera plugin from ionic-native which works fine. Now I want to use BackgroundMode plugin:https://github.com/katzer/cordova-plugin-background-mode.I read the README, I did the installation as described.

Under Usage it says that the plugin can be used like this:

cordova.plugins.backgroundMode.enable();

In my IDE (Atom), when I type that, it says it can't find cordova.

I googled a lot about cordova plugins and Ionic 2 and in some cases they usenavigator.somePlugin.someFunction() (the window.navigator object if I understand correctly) but that also doesn't work for me. I did a console.log in my app and chrome device inspector shows this:

JSON.stringify(window.navigator, null, 2)
{
  "app": {},
  "camera": {
    "DestinationType": {
      "DATA_URL": 0,
      "FILE_URI": 1,
      "NATIVE_URI": 2
    },
    "EncodingType": {
      "JPEG": 0,
      "PNG": 1
    },
    "MediaType": {
      "PICTURE": 0,
      "VIDEO": 1,
      "ALLMEDIA": 2
    },
    "PictureSourceType": {
      "PHOTOLIBRARY": 0,
      "CAMERA": 1,
      "SAVEDPHOTOALBUM": 2
    },
    "PopoverArrowDirection": {
      "ARROW_UP": 1,
      "ARROW_DOWN": 2,
      "ARROW_LEFT": 4,
      "ARROW_RIGHT": 8,
      "ARROW_ANY": 15
    },
    "Direction": {
      "BACK": 0,
      "FRONT": 1
    }
  },
  "splashscreen": {}
}

My question is:

How can I make use of the BackgroundMode plugin in ionic 2 TS? I don't even know how to include it into my project ...

解决方案

Just like AGrandt says here, you can install it with:

ionic plugin add cordova-plugin-background-mode

Then include this line after the imports:

declare var cordova:any;

And use it when the platform is ready:

platform.ready().then(
    () => {
        console.log("MyApp::constructor platform.ready");
        cordova.plugins.backgroundMode.setDefaults({ 
            title: 'My App Name', 
            text: 'Active in background...');
        cordova.plugins.backgroundMode.enable();
    }
);

本文地址:IT屋 » Using a third-party cordova plugin in Ionic 2 with TypeScript

问 题

在我的Ionic 2应用程序(TypeScript)中,我使用插件,例如从 ionic-native 的Camera插件可以正常工作。现在我想使用 BackgroundMode插件:
https://github.com/katzer/cordova-plugin-background-mode 。



在用法下,它说这个插件可以这样使用:



  cordova.plugins.backgroundMode.enable() 
 


在我的IDE(Atom)中,当我输入时,它说它找不到cordova。



我搜索了很多关于cordova插件和Ionic 2,在某些情况下他们使用
navigator.somePlugin.someFunction() code>( window.navigator 对象,如果我理解正确),但也不适用于我。我在我的应用程式和Chrome装置检查器中做了 console.log :



  JSON.stringify(window.navigator,null,2)
 {
“app”:{},
“camera”:{
“DestinationType”:{
“DATA_URL”:0,
“FILE_URI”:1,
“NATIVE_URI”:2 
},
“EncodingType”:{
“JPEG” :0,
“PNG”:1 
},
“MediaType”:{
“PICTURE”:0,
“VIDEO”:1,
“ALLMEDIA”:2 
},
“PictureSourceType”:{
“PHOTOLIBRARY”:0,
“CAMERA”:1,
“SAVEDPHOTOALBUM”:2 
},
“PopoverArrowDirection”:{
“ARROW_UP”:1,
“ARROW_DOWN”:2,
“ARROW_LEFT”:4,
“ ARROW_RIGHT“:8,
”ARROW_ANY“:15 
},
”Direction“:{
”BACK“:0,
”FRONT“:1 
} 
},
“splashscreen”:{} 
} 
  


我的问题是:



如何在ionic 2 TS中使用BackgroundMode插件?我甚至不知道如何将它包含到我的项目...


解决方案

就像 AGrandt 此处,您可以安装:



  ionic plugin add cordova-plugin-background-mode 
  


这行之后导入:



  declare var cordova:any; 
  


在平台准备就绪后使用:



  platform.ready()。then(
()=> {
 console.log(“MyApp :: constructor platform.ready”); 
 cordova.plugins.backgroundMode.setDefaults({
 title:'My App Name',
 text:'Active in background ...'); 
 cordova.plugins.backgroundMode。 enable(); 
} 
); 
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值