【问题标题】:Ionic 3 - Cordova unavailable when ran on a deviceIonic 3 - 在设备上运行时 Cordova 不可用
【发布时间】:2018-08-20 12:05:18
【问题描述】:

我想使用一些 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"

【问题讨论】:

  • 您是否尝试过创建一个新的空白 Ionic 项目并运行 ionic cordova run android -l -c --device

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


【解决方案1】:

尝试在没有 livereload (-l) 的情况下运行,有时您没有 livereload 选项的授权:

ionic cordova run android --device

【讨论】:

    【解决方案2】:

    您是否检查过 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>
    

    【讨论】:

    • 是的,cordova.js 在 &lt;head&gt; 标签中,而 main.js 在 &lt;body&gt; 的末尾
    • 它在没有实时重新加载的情况下工作吗?如果是这样,这可能是您的解决方案:stackoverflow.com/a/48266685
    猜你喜欢
    • 2019-12-16
    • 2018-06-22
    • 1970-01-01
    • 2019-11-20
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多