在 Cordova 上运行(或引导)基于 angular-cli 的应用程序的最佳方法是什么?
注意:这是针对使用了多个 Cordova 插件的 Angular 4.x 应用。
选项 A:在您的 www/index.html(www 是 Cordova 文件夹)中发布 ng build,您应该执行以下操作:
<script src="cordova.js"></script>
<script>
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
console.log('onDeviceReady has been called. Lets start loading JS files.');
var url = ['inline.bundle.js', 'polyfills.bundle.js', 'styles.bundle.js', 'vendor.bundle.js', 'main.bundle.js'];
for(var i = 0; i < url.length; i++){
loadJSFile(url[i]);
}
}
function loadJSFile(url) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
head.appendChild(script);
}
</script>
或者,
选项 B:onDeviceReady() 可以放在 main.ts(在引导 AppModule 之前)或 app.component.ts 中的某处。
我已经尝试过选项 A,但我的应用程序在 iPad 上加载的时间太长。所以我想知道我是否遵循了好的方法。提前感谢您的建议。
最佳答案
我不知道这是否是最好的方法,但在我的 main.ts 中,我添加了带有箭头函数的设备就绪事件监听器,该函数调用 Angular Bootstrap 。有用。
document.addEventListener("设备就绪", () =>
platformBrowserDynamic().bootstrapModule(AppModule), false);
关于Angular 4 + Cordova + 设备就绪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44826046/