Ionic 2.0 - 将插件添加到应用程序模块 - 语法无效?

Ionic 2.0 - Add plugins to app module - invalid syntax?

我正在尝试将 Geolocation Ionic 本机插件添加到我的 Ionic 2.0 应用程序中。

按照说明 here,我已经导入了地理定位插件,并试图将其添加到我的 AppModule 装饰器上的“提供者”数组中,但出现语法错误:

import { Geolocation } from '@ionic-native/geolocation';
...

@NgModule({
  declarations: [
    MyApp,
    BasketPage,
    AccountPage,
    AccountCreationPage,
    CategoriesPage,
    TabsPage,
    LandingPage,
    CategoryPage,
    ProductPage
  ],
  imports: [
      IonicModule.forRoot(MyApp),
      HttpModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    BasketPage,
    AccountPage,
    AccountCreationPage,
    CategoriesPage,
    TabsPage,
    LandingPage,
    CategoryPage,
    ProductPage
  ],
  providers: [
      Geolocation,
      { provide: ErrorHandler, useClass: IonicErrorHandler }
  ]
})
export class AppModule {}

providers 数组似乎只接受具有“provide”和“useClass”属性的对象,因此添加 Geolocation 对象不起作用。

此外,如果您查看 Ionic 文档,providers 数组看起来与我的不同?这是为什么?当我查看 Ionic 文档时,只有两个版本 - v1 和“最新”。

我做错了什么?

我在任务运行程序构建应用程序时收到的错误消息如下(尽管 Visual Studio 无论如何用红色突出显示语法错误):

12:56:37] typescript: …rce/Repos/Aise/Aise/AiseMobile/node_modules/@ionic-native/geolocation/index.d.ts, line: 127 Type ‘any’ is not a constructor function type. L127: export declare class Geolocation extends IonicNativePlugin {

作为参考,这里是 ionic info 命令的输出:

cli packages: (C:\Users\ciara\Source\Repos\Aise\Aise\AiseMobile\node_modules)

@ionic/cli-utils : 1.9.2 ionic (Ionic CLI) : 3.9.2 global packages:

Cordova CLI : 7.0.1 local packages:

@ionic/app-scripts : 1.1.0 Cordova Platforms : android 6.2.3 Ionic Framework : ionic-angular 2.0.1 System:

Android SDK Tools : 25.2.3 Node : v6.10.3 npm
: 3.10.10 OS : Windows 10

我的package.json

{
  "name": "io.cordova.myappeaf9db",
  "author": "",
  "homepage": "",
  "private": true,
  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve",
    "watch": "ionic-app-scripts watch"
  },
  "dependencies": {
    "@angular/common": "2.2.1",
    "@angular/compiler": "2.2.1",
    "@angular/compiler-cli": "2.2.1",
    "@angular/core": "2.2.1",
    "@angular/forms": "2.2.1",
    "@angular/http": "2.2.1",
    "@angular/platform-browser": "2.2.1",
    "@angular/platform-browser-dynamic": "2.2.1",
    "@angular/platform-server": "2.2.1",
    "@ionic-native/geolocation": "^4.2.1",
    "@ionic/storage": "1.1.7",
    "cordova-android": "^6.2.3",
    "cordova-plugin-compat": "^1.0.0",
    "cordova-plugin-console": "1.0.5",
    "cordova-plugin-device": "1.1.4",
    "cordova-plugin-geolocation": "^2.4.3",
    "cordova-plugin-splashscreen": "~4.0.1",
    "cordova-plugin-statusbar": "2.2.1",
    "cordova-plugin-whitelist": "1.3.1",
    "ionic-angular": "2.0.1",
    "ionic-native": "2.4.1",
    "ionic-plugin-keyboard": "~2.2.1",
    "ionicons": "3.0.0",
    "jsonpath": "0.2.12",
    "rxjs": "5.0.0-beta.12",
    "sw-toolbox": "3.4.0",
    "zone.js": "0.6.26"
  },
  "devDependencies": {
    "@ionic/app-scripts": "1.1.0",
    "@ionic/cli-plugin-ionic-angular": "1.2.0",
    "typescript": "2.0.9"
  },
  "description": "Ionic2Tabs: An Ionic project",
  "cordovaPlugins": [
    "cordova-plugin-whitelist",
    "cordova-plugin-console",
    "cordova-plugin-statusbar",
    "cordova-plugin-device",
    "cordova-plugin-splashscreen",
    "ionic-plugin-keyboard"
  ],
  "cordovaPlatforms": [],
  "-vs-binding": {
    "BeforeBuild": [
      "ionic:build"
    ],
    "ProjectOpened": [
      "watch"
    ]
  },
  "cordova": {
    "plugins": {
      "cordova-plugin-console": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-whitelist": {},
      "ionic-plugin-keyboard": {},
      "cordova-plugin-geolocation": {}
    },
    "platforms": [
      "android"
    ]
  }
}

如果您打算使用 ionic-native 2.x,则不需要安装 ionic-native/<pluginName> 包。

在 2.x 版本中,所有原生插件包装器都包含在单核包中。此外,所有插件类型都是 globalstatic.

在你的例子中,用法是

import { Geolocation } from ionic-native // import

//To access the functions,

Geolocation.functionName()

您可以卸载 ionic-native/Geolocation 软件包。
您也不必在 AppModule 中设置提供程序或在构造函数中注入。