8

在我的 Ionic 2 应用程序(TypeScript)中,我使用插件,例如ionic-native可以正常工作的相机插件。现在我想使用BackgroundMode plugin: https://github.com/katzer/cordova-plugin-background-mode。我阅读了自述文件,按照描述进行了安装。

在 Usage 它说插件可以像这样使用:

cordova.plugins.backgroundMode.enable();

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

我在 Google 上搜索了很多关于 cordova 插件和 Ionic 2 的信息,在某些情况下它们使用 navigator.somePlugin.someFunction()window.navigator如果我理解正确的话就是这个对象),但这对我也不起作用。我console.log在我的应用程序中做了一个,chrome 设备检查器显示了这个:

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 插件?我什至不知道如何将它包含在我的项目中......

4

1 回答 1

14

就像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();
    }
);
于 2016-06-21T11:50:00.760 回答