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

h9a6wy2h  于 2022-12-09  发布在  Ionic
关注(0)|答案(2)|浏览(151)

插件:

cordova plugin add cordova-plugin-networkinterface

编码:

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

错误:

TypeError: Cannot read property 'getIPAddress' of undefined

这里我既没有提供任何提供者,也没有提供任何导入,那么它在离子2中是如何工作的呢?
这不是一个本机插件。但这个插件是工作在离子2也根据这个link
包. 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);
    });
 }
jxct1oxe

jxct1oxe1#

你必须使用它像下面安装后的非本机插件。
这个方法(getIpAddress())已被取代,它使用getWiFiIPAddress()方法。

declare let networkinterface: any;

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

          ...

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

bq8i3lrv2#

您可以尝试使用外部URL:

async function getIP(){

  const repsIP = await fetch('https://api.ipify.org/?format=json');

  const data = await repsIP.json();

  return data;

  }

无论在何处,都可以调用此函数:

this.getIP().then(data => this.ipAddress = data.ip);

相关问题