如何在Cordova/Ionic项目中的deviceready事件中注册代码? [英] How to register code with the deviceready event in a Cordova / Ionic project?
问题描述
我要接管一个Cordova/Ionic项目.我以前从未与Cordova或Ionic一起工作过,所以我是该领域的初学者.但是,我已经和Node一起工作了几年,所以我对此最了解.
I am taking over a Cordova/Ionic project. I've never worked with Cordova or Ionic before, so I am complete beginner in that area. However, I have worked with Node, on and off, for a few years, so I mostly know about that.
一个简单的开始任务,我需要添加Appsee:
A simple starting task, I need to add 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
./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在哪里:
So I ran grep to find out where deviceready is:
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
内的任何内容?难道不是Ionic/Cordova所产生的全部东西吗?
So I only see "deviceready" inside the build
folder. I think I'm supposed to avoid editing anything inside of build
? Isn't that full of stuff that's generated by Ionic/Cordova?
我在哪里向deviceready注册某些东西?
Where do I register something with 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
我很乐意遵循其他地方的指示.
I'm happy to follow directions from elsewhere.
推荐答案
您应该使用平台在IONIC中的app.component.ts
You should use Platform to get device ready event in IONIC, In your app.component.ts
-
从"ionic-angular"导入{Platform};
import { Platform } from 'ionic-angular';
在构造函数中添加platform.ready()方法,如下所示
Add platform.ready() method inside the constructor as shown below
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();
});
}
它将在设备/平台就绪时触发. 此处是文档
it will be triggered when device/platform is ready. Here is the documentation
这篇关于如何在Cordova/Ionic项目中的deviceready事件中注册代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!