Angular 4 + Cordova +设备就绪

时间:2017-06-29 13:07:29

标签: angular cordova angular-cli

在Cordova上运行(或引导)基于角度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上加载时间过长。所以我想知道我是否遵循了良好的方法。提前感谢您的建议。

3 个答案:

答案 0 :(得分:10)

我不知道这是否是最好的方法,但在我的main.ts中,我添加了带有箭头函数的deviceready事件监听器,该函数调用了角度引导程序。有用。

MessageQueue queue = new MessageQueue("FormatName:Direct=TCP:my_ip\private$\msmqname_name"); queue.Formatter = new BinaryMessageFormatter(FormatterAssemblyStyle.Simple, FormatterTypeStyle.typesWhenNeeded); System.Messaging.Message msg = new System.Messaging.Message(); msg.BodyStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes("message")); queue.send(msg,"LABEL");

答案 1 :(得分:5)

如果您还需要为Cordova添加支票,则需要更复杂的代码。否则Angular会抱怨'试图找到引导代码,但不能。指定静态可分析引导代码或将entryModule传递给插件选项。'

const bootstrap = () => {
  platformBrowserDynamic().bootstrapModule(AppModule);
};

if (typeof window['cordova'] !== 'undefined') {
  document.addEventListener('deviceready', () => {
    bootstrap();
  }, false);
} else {
  bootstrap();
}

答案 2 :(得分:0)

在main.ts或main-aot.ts取决于你如何调用main.ts文件

 document.addEventListener('deviceready', () => {
    console.log("bootstrap device");
    platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
  }, false);
}