在ionic2中获取路由器的IP地址 - 非本机插件

时间:2017-04-09 07:48:39

标签: ionic-framework ionic2 network-interface

插件:

cordova plugin add cordova-plugin-networkinterface

代码:

networkinterface.getIpAddress((ip) => {
    console.log(ip);
});

错误:

TypeError: Cannot read property 'getIPAddress' of undefined

现在我既没有提供任何提供商也没有提供任何导入,那么它在离子2中是如何工作的? 它不是本机插件。但是根据这个link

,这个插件也在离子2中工作

Package.json:

{
  "name": "ionic-hello-world",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "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"
},
"dependencies": {
"@angular/common": "2.4.8",
"@angular/compiler": "2.4.8",
"@angular/compiler-cli": "2.4.8",
"@angular/core": "2.4.8",
"@angular/forms": "2.4.8",
"@angular/http": "2.4.8",
"@angular/platform-browser": "2.4.8",
"@angular/platform-browser-dynamic": "2.4.8",
"@angular/platform-server": "2.4.8",
"@ionic-native/core": "3.1.0",  
"@ionic-native/in-app-browser": "^3.4.2",
"@ionic-native/network": "^3.4.2",
"@ionic-native/splash-screen": "3.1.0",
"@ionic-native/status-bar": "3.1.0",
"@ionic-native/toast": "^3.4.4",
"@ionic/app-scripts": "^1.2.2",
"@ionic/storage": "2.0.0",
"ionic-angular": "2.3.0",
"ionic-native": "^2.9.0",
"ionicons": "3.0.0",
"rxjs": "5.0.1",
"sw-toolbox": "3.4.0",
"zone.js": "0.7.2"
},
"devDependencies": {
  "@ionic/app-scripts": "1.2.2",
  "typescript": "2.0.9"
},
"cordovaPlugins": [
"cordova-plugin-whitelist",
"cordova-plugin-statusbar",
"cordova-plugin-console",
"cordova-plugin-device",
"cordova-plugin-splashscreen",
"ionic-plugin-keyboard"
   ],
  "cordovaPlatforms": [],
   "description": "Test"
}

最新代码文件:

declare var networkinterface: any;

    @Component({
     templateUrl: 'app.html'
   })
   export class MyApp {
     public rootPage:any = HomePage;

    constructor(private platform: Platform,private statusBar: StatusBar,private splashScreen: SplashScreen, private network: Network, private iab: InAppBrowser, private http: Http, private toast: Toast) {

    platform.ready().then(() => {
      networkinterface.getWiFiIPAddress((ip) => {
                console.log(ip);
    });
 }  

1 个答案:

答案 0 :(得分:0)

安装非本机插件后,您必须像下面一样使用它。

  

不推荐使用此方法(getIpAddress())并使用   getWiFiIPAddress()方法。

     declare let networkinterface: any;

        @Component({
          ...
        })
        export class TestPage {

          ...

          getIpAddress() {
            networkinterface.getWiFiIPAddress((ip) => {
            console.log(ip);
        });
      }
    }