【问题标题】:ionic framework network plugin issue离子框架网络插件问题
【发布时间】:2019-07-12 21:18:15
【问题描述】:

我正在使用 ionic 3。我安装了网络插件来检查 app.component.ts 中的网络连接

https://ionicframework.com/docs/native/network/

但是当我使用这种方法时,它给了我错误。

core.js:1449 ERROR Error: Uncaught (in promise): TypeError: Object(...) is not a function
TypeError: Object(...) is not a function
    at Network.onDisconnect (index.js:61)
    at app.component.ts:17
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4760)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.js:4751)
    at t.invokeTask (polyfills.js:3)
    at c (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.js:4751)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)
    at e.invokeTask [as invoke] (polyfills.js:3)
    at p (polyfills.js:2)
    at HTMLDocument.v (polyfills.js:2)

而我的 app.component.ts 的代码是

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private network: Network) {
    platform.ready().then(() =>
    {
                this.network.onDisconnect().subscribe(() =>
          {
            console.log("onDisconnect");
          });

           this.network.onConnect().subscribe(() =>
           {
             console.log("onConnect");
           });

      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
    });
  }

我的 package.json

{
  "name": "testNetwork1",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "start": "ionic-app-scripts serve",
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint"
  },
  "dependencies": {
    "@angular/animations": "5.2.11",
    "@angular/common": "5.2.11",
    "@angular/compiler": "5.2.11",
    "@angular/compiler-cli": "5.2.11",
    "@angular/core": "5.2.11",
    "@angular/forms": "5.2.11",
    "@angular/http": "5.2.11",
    "@angular/platform-browser": "5.2.11",
    "@angular/platform-browser-dynamic": "5.2.11",
    "@ionic-native/core": "~4.18.0",
    "@ionic-native/network": "^5.2.0",
    "@ionic-native/splash-screen": "~4.18.0",
    "@ionic-native/status-bar": "~4.18.0",
    "@ionic/pro": "2.0.4",
    "@ionic/storage": "2.2.0",
    "cordova-plugin-network-information": "2.0.1",
    "ionic-angular": "3.9.3",
    "ionicons": "3.0.0",
    "rxjs": "5.5.11",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.29"
  },
  "devDependencies": {
    "@ionic/app-scripts": "^3.2.3",
    "typescript": "~2.6.2"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-plugin-network-information": {}
    }
  }
}

【问题讨论】:

  • 你的 package.json 包含什么?
  • 请检查我是否更新了我的 package.json @SurajRao

标签: ionic-framework ionic3 ionic-native network-connection


【解决方案1】:

您正在使用 ionic 3/angular 5.x

您需要参考ionic v3 documentation 并使用ionic native v4

npm install --save @ionic-native/network@4

确保删除版本 5.x ionic-native/network 包装器

您的导入将是:

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

【讨论】:

  • 所有离子原生插件都存在同样的问题。 5.x 版本和 ngx 导入适用于 ionic 4/ angular 7.x(ngx 是 angular 7)。您的其他包装器似乎仅在 4.x 中,因此它们正在工作
  • 请帮我解决这个问题stackoverflow.com/questions/58852575/…@Suraj Rao
【解决方案2】:

这里是当您的应用在线或离线时检查互联网连接的示例代码。

首先,您需要创建网络提供商并添加以下代码,

import { Injectable } from '@angular/core';
import { AlertController, Events } from 'ionic-angular';
import { Network } from '@ionic-native/network';

export enum ConnectionStatusEnum {
    Online,
    Offline
}


@Injectable()
export class NetworkProvider {

  previousStatus;

  constructor(public alertCtrl: AlertController, 
              public network: Network,
              public eventCtrl: Events) {

    console.log('Hello NetworkProvider Provider');

    this.previousStatus = ConnectionStatusEnum.Online;

  }

    public initializeNetworkEvents(): void {
        this.network.onDisconnect().subscribe(() => {
            if (this.previousStatus === ConnectionStatusEnum.Online) {
                this.eventCtrl.publish('network:offline');
            }
            this.previousStatus = ConnectionStatusEnum.Offline;
        });
        this.network.onConnect().subscribe(() => {
            if (this.previousStatus === ConnectionStatusEnum.Offline) {
                this.eventCtrl.publish('network:online');
            }
            this.previousStatus = ConnectionStatusEnum.Online;
        });
    }

}

现在在初始化您的应用时将此代码添加到 app.component 文件中,

import { Component } from '@angular/core';
import { Platform,  Events } from 'ionic-angular';
import { Network } from '@ionic-native/network';
import { NetworkProvider } from '../providers/network/network';

@Component({
    templateUrl: 'app.html'
})

export class MyApp {

        constructor(public platform: Platform, 
                    public events: Events,
                    public network: Network,
                    public networkProvider: NetworkProvider) { }

        initializeApp() {

            this.platform.ready().then(() => {

                this.networkProvider.initializeNetworkEvents();

                // Offline event
                this.events.subscribe('network:offline', () => {
                    alert('network:offline ==> '+this.network.type);    
                });

                // Online event
                this.events.subscribe('network:online', () => {
                    alert('network:online ==> '+this.network.type);        
                });

            });
        }

}

【讨论】:

  • 我也尝试过同样的错误未捕获(在承诺中):TypeError:Object(...)不是函数 TypeError:Object(...)不是网络上的函数。 NetworkProvider.webpackJsonp.340.NetworkProvider.initializeNetworkEvents (network.ts:36) 处的 onDisconnect (index.js:61)
  • 我共享的示例代码对我有用。尝试创建网络提供商它可能会解决您的问题或在您的查询中发布更新的组件代码以查看。
  • 我也创建了一个网络提供商,但无法解决这个问题,出现同样的错误
  • 尝试将网络导入为 import { Network } from '@ionic-native/network/ngx';
猜你喜欢
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 2015-10-28
相关资源
最近更新 更多