You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic 3使用Network插件检测网络连接时触发Object(...)类型错误

问题分析与解决方案

你遇到的TypeError: Object(...) is not a function错误,核心原因是**@ionic-native插件版本不兼容**。从你的package.json配置能明显看到版本冲突:

  • @ionic-native/core是~4.18.0(属于适配Ionic 3的4.x系列)
  • @ionic-native/network却是^5.2.0(5.x系列仅支持Ionic 4及更高版本)

版本不匹配导致插件的调用逻辑和核心库无法兼容,进而触发了这个错误。


你遇到的错误信息:

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)

解决步骤

1. 卸载当前不兼容的Network插件

在项目根目录执行命令:

npm uninstall @ionic-native/network

2. 安装与core版本匹配的4.x系列Network插件

执行命令安装和你的@ionic-native/core版本一致的4.18.0版本:

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

3. 重新构建项目,确保插件生效

ionic cordova prepare

验证你的代码

你的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();
  });
}

补充说明

Ionic 3对应的所有@ionic-native插件都必须保持在4.x版本,5.x及以上版本仅适配Ionic 4+。以后安装插件时记得指定对应版本号,避免再出现类似的版本冲突问题。

内容的提问来源于stack exchange,提问作者Himanshu itmca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 03:47:33