Ionic 3 - Cordova在设备上运行时不可用

时间:2018-03-12 12:52:45

标签: cordova ionic-framework ionic3 cordova-plugins ionic-native

我想使用一些Ionic Native插件,但我无法对它们进行测试。

这是代码:

// app.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Push, PushObject, PushOptions } from '@ionic-native/push';

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

    constructor(private push: Push, platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
        platform.ready().then(() => {
            // 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();

            // Add Push Notifications
            push.hasPermission().then((response: any) => {
                if (response.isEnabled) {
                    console.info("Push notification permission granted");
                } else {
                    console.error("Push notifications permission needed");
                }
            });
        });
    }

}

当我运行ionic cordova run android -l -c --device时,我会收到以下日志:

  

[13:39:15] console.warn:Native:尝试调用StatusBar.styleDefault,但Cordova不可用。确保包含cordova.js或在设备/模拟器中运行

     

[13:39:15] console.warn:Native:尝试调用SplashScreen.hide,但Cordova不可用。确保包含cordova.js或在设备/模拟器中运行

在应用程序中:

  

未捕获(承诺):cordova_not_available

this.platform.is('cordova')false(与this.platform.is('android')相同)

指定--device应该使cordova可用吗?即使使用--emulator,我也会遇到同样的错误。 那么,出了什么问题?

这是我的ionic info

cli packages: (C:\Users\Pierre\AppData\Roaming\npm\node_modules)
    @ionic/cli-utils  : 1.19.1
    ionic (Ionic CLI) : 3.19.1

global packages:
    cordova (Cordova CLI) : 8.0.0

local packages:
    @ionic/app-scripts : 3.1.8
    Cordova Platforms  : android 7.0.0
    Ionic Framework    : ionic-angular 3.9.2

System:
    Android SDK Tools : 26.1.1
    Node              : v8.9.4
    npm               : 5.6.0
    OS                : Windows 10

我的ionic cordova plugin --list

cordova-plugin-camera 4.0.2 "Camera"
cordova-plugin-device 2.0.1 "Device"
cordova-plugin-file 6.0.1 "File"
cordova-plugin-file-transfer 1.7.1 "File Transfer"
cordova-plugin-ionic 4.0.0 "IonicCordova"
cordova-plugin-ionic-keyboard 2.0.5 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 1.1.16 "cordova-plugin-ionic-webview"
cordova-plugin-splashscreen 5.0.2 "Splashscreen"
cordova-plugin-whitelist 1.3.3 "Whitelist"
phonegap-plugin-push 2.2.2 "PushPlugin"

2 个答案:

答案 0 :(得分:1)

您是否检查过index.html中是否包含cordova.js?如果是这样,它是否在其他脚本之前加载?

<!-- Ionic's root component andionic cordova platform remove ios where the app will load -->
<ion-app></ion-app>

<!-- cordova.js required for cordova apps -->
<script src="cordova.js"></script>

<!-- The polyfills js is generated during the build process -->
<script src="build/polyfills.js"></script>

<!-- all code from node_modules directory is here -->
<script src="build/vendor.js"></script>

<!-- The bundle js is generated during the build process -->
<script src="build/main.js"></script>

答案 1 :(得分:0)

尝试在没有livereload(-l)的情况下运行,有时候你没有使用livereload选项进行授权:

ionic cordova run android --device