在 Ionic 2 中使用第三方 cordova 插件和 TypeScript [英] 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
.我阅读了 README,按照说明进行了安装.
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();
在我的 IDE (Atom) 中,当我输入它时,它说找不到cordova.
In my IDE (Atom), when I type that, it says it can't find cordova.
我在 Google 上搜索了很多关于 cordova 插件和 Ionic 2 的信息,在某些情况下他们使用navigator.somePlugin.someFunction()
(如果我理解正确,则为 window.navigator
对象),但这对我也不起作用.我在我的应用程序中做了一个 console.log
并且 chrome 设备检查器显示了这个:
I googled a lot about cordova plugins and Ionic 2 and in some cases they use
navigator.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": {}
}
我的问题是:
如何在 ionic 2 TS 中使用 BackgroundMode 插件?我什至不知道如何将它包含到我的项目中......
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();
}
);
这篇关于在 Ionic 2 中使用第三方 cordova 插件和 TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!