【问题标题】:Ionic 2.0 - Add plugins to app module - invalid syntax?Ionic 2.0 - 向应用程序模块添加插件 - 语法无效?
【发布时间】:2018-02-08 03:58:00
【问题描述】:

我正在尝试将 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] 打字稿: …rce/Repos/Aise/Aise/AiseMobile/node_modules/@ionic-native/geolocation/index.d.ts, 行:127 类型‘any’不是构造函数类型。 L127:出口 声明类 Geolocation 扩展 IonicNativePlugin {

作为参考,下面是 ionic info 命令的输出:

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

@ionic/cli-utils : 1.9.2 ionic (Ionic CLI) : 3.9.2 全局包:

Cordova CLI:7.0.1 本地包:

@ionic/app-scripts:1.1.0 Cordova 平台:android 6.2.3 Ionic 框架:ionic-angular 2.0.1 系统:

Android SDK 工具:25.2.3 节点:v6.10.3 npm
:3.10.10 操作系统: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 3.0 - 我希望暂时使用 Ionic 2.0,以避免我的任何升级问题现有的应用程序。另一个问题似乎与我也失败的同一段代码(AppModule 类)无关。
  • 在这种情况下,您的地理位置包装器必须是 3.x + .. 您可能需要使用较旧的 ionic-native 2.x,您根本不需要作为提供者注入。你能展示你的 package.json 吗?

标签: ionic-framework ionic2 ionic-native


【解决方案1】:

如果您打算使用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 中设置提供者或在构造函数中注入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多