首页 文章

如何在Cordova / Ionic项目中使用deviceready事件注册代码?

提问于
浏览
1

我正在接管Cordova / Ionic项目 . 我之前从未与Cordova或Ionic合作过,所以我在那个领域是初学者 . 但是,我已经和Node一起工作了几年,所以我大多都知道这一点 .

一个简单的启动任务,我需要添加Appsee:

https://www.appsee.com/docs/ios/ionic

这部分很简单:

In case you're using TypeScript (default in ionic 2 and ionic 3) place the following line under the imports:

declare var Appsee:any;

我把这个放在这个文件中:

./src/app/app.component.ts

但这部分不太明显:

Call the following method when your app starts, preferably when the deviceready event fires:

Appsee.start("YOUR API KEY");

所以我运行grep来找出deviceready的位置:

grep -iR "deviceready" *   | grep -v node_modules

www/build/vendor.js:     * resolve when Cordova triggers the `deviceready` event.
www/build/vendor.js:            // prepare a custom "ready" for cordova "deviceready"
www/build/vendor.js:                    doc.addEventListener('deviceready', function () {
www/build/vendor.js:                        // 3) cordova deviceready event triggered
www/build/vendor.js:    var deviceReady = new Promise(function (resolve, reject) {
www/build/vendor.js:            document.addEventListener("deviceready", function () {
www/build/vendor.js:    var deviceReadyDone = deviceReady.catch(function () {
www/build/vendor.js:        return deviceReadyDone.then(function () {
www/build/vendor.js:    document.addEventListener('deviceready', function () {
www/build/vendor.js:        console.log("Ionic Native: deviceready event fired after " + (Date.now() - before) + " ms");
www/build/vendor.js:            console.warn("Ionic Native: deviceready did not fire within " + DEVICE_READY_TIMEOUT + "ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.");

所以我只在 build 文件夹中看到"deviceready" . 我想我应该避免编辑 build 内的任何内容? Isn 't that full of stuff that'由Ionic / Cordova生成?

我在哪里注册deviceready?

如果我跑:

ionic info

我明白了:

[WARN] Detected locally installed Ionic CLI, but it's too old--using global CLI.

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.19.2
    ionic (Ionic CLI) : 3.20.0

global packages:

    cordova (Cordova CLI) : 8.0.0 

local packages:

    @ionic/app-scripts : 2.1.4
    Cordova Platforms  : none
    Ionic Framework    : ionic-angular 3.6.0

System:

    ios-deploy : 1.9.2 
    Node       : v6.5.0
    npm        : 3.10.3 
    OS         : OS X El Capitan
    Xcode      : Xcode 7.3.1 Build version 7D1014 

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : pro

我很乐意遵循其他地方的指示 .

1 回答

  • 2

    您应该使用 Platform 在IONIC中的app.component.ts中获取设备就绪事件

    从'ionic-angular'导入;在构造函数中添加platform.ready()方法,如下所示

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
        platform.ready().then(() => {
          // Okay, so the platform is ready.
          // Here you can do any higher level native things you might need.
          Appsee.start("YOUR API KEY");
          statusBar.styleDefault();
          splashScreen.hide();
        });
    

    }

    它将在设备/平台准备就绪时触发 . Here is the documentation

相关问题