Wenn ich ionic app mit ionic serve -l
ausführe, bekomme ich diesen Fehler:
Runtime Error
Ungefangen (in promise): cordova_not_available
Stack
Error: Uncaught (in promise): cordova_not_available
at v (http://localhost:8100/build/polyfills.js:3:4864)
at s (http://localhost:8100/build/polyfills.js:3:4289)
at s (http://localhost:8100/build/polyfills.js:3:4112)
at http://localhost:8100/build/polyfills.js:3:4652
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:10284)
at Object.onInvokeTask (http://localhost:8100/build/main.js:38692:37)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:10220)
at e.runTask (http://localhost:8100/build/polyfills.js:3:7637)
at i (http://localhost:8100/build/polyfills.js:3:3707)
at HTMLDocument.invoke (http://localhost:8100/build/polyfills.js:3:11437)
andere Details
Ionic Framework: 2.2.0
Ionic Native: 2.8.1
Ionic App Scripts: 1.1.4
Angular Core: 2.4.8
Angular Compiler CLI: 2.4.8
Node: 6.9.2
OS Platform: Windows 10
Navigator Platform: Win32
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Ich installiere das Plugin HotSpot mit folgendem Befehl:
ionic plugin add cordova-plugin-hotspot --save
cordova plugin add cordova-plugin-hotspot --save
Benutzung app.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen,Hotspot,Network } from 'ionic-native';
import { TabsPage } from '../pages/tabs/tabs';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage = TabsPage;
constructor(platform: Platform) {
platform.ready().then(() => {
StatusBar.styleDefault();
Splashscreen.hide();
Hotspot.scanWifi().then((networks: Array<Network>) => {
console.log(networks);
});
})
}
}
Ich sehe auch andere Fragen, die aber mein Problem nicht lösen.
Sie greifen auf native Plugins zu, während Sie im Browser testen. Damit die Plugins funktionieren, sollten Sie ein echtes Gerät zum Testen verwenden.
Um Ihren Code im Browser testbar zu machen (oder tatsächlich nicht zu brechen, wenn Sie im Browser testen), sollten Sie eine if-Anweisung haben, die überprüft, ob Cordova verfügbar ist:
if (this.platform.is('cordova')) {
// You're on a device, call the native plugins. Example:
//
// var url: string = '';
//
// Camera.getPicture().then((fileUri) => url = fileUri);
} else {
// You're testing in browser, do nothing or mock the plugins' behaviour.
//
// var url: string = 'assets/mock-images/image.jpg';
}
EDIT:
Wie Ricky Levi hier unten richtig erwähnt, unterstützt Ionic die Browser
-Plattform. Mit dieser Plattform können die meisten gängigen Plugins funktionieren. Beachten Sie, dass einige Plugins nicht funktionieren, zum Beispiel das Barcode-Scanner-Plugin. Es wird Sie mit einem Alert nach dem Wert fragen, der "gescannt" werden soll. Dadurch geht der gesamte Anwendungsfall eines Barcode-Scanners verloren.
Cordova ist nur zugänglich, wenn Sie Ihre Anwendung auf einem echten Gerät ausführen. Wenn Sie Ihre App im Browser testen, kann sie nicht auf diese nativen Plugins zugreifen.
Sie können überprüfen, ob Sie sich auf einem echten Gerät oder einem solchen Browser befinden:
if (this.platform.is('cordova')) {
// You are on a device, cordova plugins are accessible
} else {
// Cordova not accessible, add mock data if necessary
}
Damit können Sie nur die Teile Ihrer Anwendung testen, die nicht auf Cordova-Plugins angewiesen sind. Um Ihre App wirklich zu testen, müssen Sie sie auf einem Gerät oder im Emulator ausführen.