2017-04-09 61 views
2

插件:获取路由器的IP地址 - 非本地插件

cordova plugin add cordova-plugin-networkinterface 

代码:

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

错误:

TypeError: Cannot read property 'getIPAddress' of undefined 

现在,这里既不是我提供任何提供商也没有任何进口,所以它如何工作k在离子2?
这不是一个本地插件。但这个插件离子2还根据该link

的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); 
    }); 
} 
+0

你能告诉你的'package.json'? – Sampath

+0

你使用本地插件还是什么?它的URL? – Sampath

+0

它不是本地插件。 [链接](https://github.com/salbahra/cordova-plugin-networkinterface) –

回答

0

你可以选择安装后不使用它像下面 - 原生插件。

This method (getIpAddress()) is deprecated and uses the getWiFiIPAddress() method.

 declare let networkinterface: any; 

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

      ... 

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

error_handler.js:54 EXCEPTION:未捕获(承诺):ReferenceError:未定义网络接口 ReferenceError:网络接口未定义 –

+0

您可以将最新的代码放在您的帖子上面吗? – Sampath

+0

使用'let'而不是'var'。 – Sampath