2016-06-21 62 views
8

在我的Ionic 2应用程序(TypeScript)中,我使用插件,例如ionic-native的Camera插件正常工作。现在我想用BackgroundMode pluginhttps://github.com/katzer/cordova-plugin-background-mode。 我阅读了自述文件,我按照说明进行了安装。在Typeonic中使用Ionic 2中的第三方cordova插件

在使用它说,该插件可以这样使用:

cordova.plugins.backgroundMode.enable(); 

在我的IDE(凌动),当我键入,它说,它无法找到科尔多瓦。

我用Google搜索了很多关于科尔多瓦插件和离子2在某些情况下,他们使用 navigator.somePlugin.someFunction()但也不会为我工作(如果我理解正确的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": {} 
} 

我的问题是:

我怎样才能使离子2个TS使用BackgroundMode插件?我甚至不知道如何把它列入到我的项目...

回答

12

就像AGrandthere,你可以安装它:

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(); 
    } 
); 
+1

你能解释更多信息,请W¯¯这里应该去“声明var cordova:任何”???我没有得到它,既没有下一个代码“platform.ready().....”在此先感谢! –

相关问题