Ionic的Network本机插件缺少的提供程序

时间:2019-05-09 10:33:30

标签: typescript cordova ionic-framework npm

我尝试使用Ionics本机插件“网络”,但是由于据说缺少提供程序而失败。为了避免出现任何错误,我已经安装了Ionic的全新安装以及所需的依赖项:

ionic cordova plugin add cordova-plugin-network-information
npm install @ionic-native/network

然后我将依赖项添加到home的构造函数中:

import { Component } from '@angular/core';
import { Network } from '@ionic-native/network/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  constructor(private network: Network) {
      // to nothing ...
  }
}

...,并且在执行ionic serve后将收到以下错误消息:

ERROR Error: Uncaught (in promise): 
Error: StaticInjectorError(AppModule)[HomePage -> Network]: 
  StaticInjectorError(Platform: core)[HomePage -> Network]: 
    NullInjectorError: No provider for Network!
Error: StaticInjectorError(AppModule)[HomePage -> Network]: 
  StaticInjectorError(Platform: core)[HomePage -> Network]: 
    NullInjectorError: No provider for Network!

系统信息

Ionic:

   ionic (Ionic CLI)             : 4.12.0 (/Users/samnowakowski/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.4.0
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 7.3.9
   @ionic/angular-toolkit        : 1.5.1

Cordova:

   cordova (Cordova CLI) : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms     : none
   Cordova Plugins       : no whitelisted plugins (1 plugins total)

System:

   Android SDK Tools : 26.1.1 (/Users/admin/Library/Android/sdk)
   NodeJS            : v10.15.3 (/usr/local/bin/node)
   npm               : 6.9.0
   OS                : macOS Mojave
   Xcode             : Xcode 10.2.1 Build version 10E1001

1 个答案:

答案 0 :(得分:1)

打开应用程序模块(app.module.ts)及以下代码:

import { Network } from '@ionic-native/network/ngx';

@NgModule(
   ...
   providers: [
      Network
   ]
);