简体   繁体   English

“loadURL” electron-Angular project only once Angular 编译器准备就绪

[英]"loadURL" electron-Angular project only once Angular Compiler is ready

In many electron-Angular tutorials you create the window and load index.html from localhost after a timeout.在许多 electron-Angular 教程中,您创建 window 并在超时后从本地主机加载 index.html。 Typically, you'll find text like this:通常,您会发现这样的文本:

// set timeout to render the window not until the Angular // compiler is ready to show the project // 设置超时以渲染 window,直到 Angular // 编译器准备好显示项目

Now I think there must be a way to not just arbitrarily wait for ten seconds, but to have some kind of event to act upon.现在我认为必须有一种方法,不仅可以任意等待十秒钟,而且可以根据某种事件采取行动。

Unfortunately, loadURL returns "true" even if the localhost is not yet reachable, so win.once('ready-to-show') is also called when, actually, it's not ready to show.不幸的是,即使 localhost 尚不可访问,loadURL 也会返回“true”,因此 win.once('ready-to-show') 也会在实际上尚未准备好显示时调用。

Ideally, I would like to have a loading screen until the Angular Compiler is ready.理想情况下,我希望有一个加载屏幕,直到 Angular 编译器准备就绪。 Any ideas?有任何想法吗? Thank you!谢谢!

As I couldn't come up with an alternative I implemented this solution: 由于无法提出替代方案,因此我实施了以下解决方案:

checkForLoad = setInterval(function () {
                if (!didLoad) {
                    win.webContents.reload();
                }
                else {
                    clearInterval(checkForLoad);
                    win.show();
                }
            }, 2000);

I reload the page every few seconds. 我每隔几秒钟重新加载一次页面。 In Angular, I just send and IPC Message to change "didLoad" to true once the Angular App is actually running. 在Angular中,我只发送和IPC消息即可在Angular App实际运行后将“ didLoad”更改为true。 Works nicely, but obviously it would be better to have an event to act upon. 效果很好,但是显然最好有一个事件可以执行。 In the meantime, it's close enough. 同时,它已经足够接近了。

I found following solution:我找到了以下解决方案:

In angular AppComponent in ngOnInit I call global method:在 angular AppComponentngOnInit中,我调用了全局方法:

window.electron.appControl.isReady()

which call哪个电话

isReady: () => ipcRenderer.send('appControl', AppControlEvents.IsReady),

and in electron main.ts I set flag for this event在 electron main.ts 中,我为此事件设置了标志

ipcMain.on('appControl', (event, eventType) => {
  switch (eventType) {
    ...
    case AppControlEvents.IsReady:
       this.isReadyHandler();
       break;
  }
})

inside this handler we are sure that angular is loaded在此处理程序中,我们确定已加载 angular

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM