经过几个小时的挣扎,我希望能找到能理解这一点的人 . Nativescript为Angular Typescript的应用程序事件提供支持:
http://docs.nativescript.org/angular/core-concepts/application-management.html
但是,我不明白我应该在哪里添加这些 . 网站上的示例显示以下代码:
import application = require("application");
application.on(application.launchEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an android.content.Intent class.
console.log("Launched Android application with the following intent: " + args.android + ".");
} else if (args.ios !== undefined) {
// For iOS applications, args.ios is NSDictionary (launchOptions).
console.log("Launched iOS application with options: " + args.ios);
}
});
...
application.start({ moduleName: "main-page" });
如何在我的Angular应用程序中使用它?对我而言,感觉这实际上并不是Angular版本的代码,但也许我错过了一些东西 .
我尝试导入应用程序并将其添加到我的主AppComponent的ngOnInit中:
ngOnInit() {
application.on(application.launchEvent, function (args: application.ApplicationEventData) {
if (args.android) {
// For Android applications, args.android is an android.content.Intent class.
console.log("Alert something"); // : " + args.android + ".");
} else if (args.ios !== undefined) {
// For iOS applications, args.ios is NSDictionary (launchOptions).
console.log("Launched iOS application with options: " + args.ios);
}
console.log('launch detected')
});
console.log('application initiated');
}
虽然我没有收到任何错误,但我只能看到应用程序启动的消息 . 关于如何使用提到的android args的任何想法?
Update: 最后它比预期的要容易得多,请看我用来解决这个问题的代码:
import app = require('application');
import "reflect-metadata";
import {nativeScriptBootstrap} from "nativescript-angular/application";
import {AppComponent} from "./app.component";
app.on(app.launchEvent, function (args: app.ApplicationEventData) {
// on launch code
});
nativeScriptBootstrap(AppComponent);
谢谢您的帮助!
1 回答
您需要将应用程序事件函数添加到main.ts或app.ts文件中,然后再与Bootstrap一起添加