Angular 7/Electron: Routing does not complete after BrowserWindow.hide followed by BrowserWindow.show

Viewed 665

I have an Angular 7 application that runs in an Electron window. The Electron process in main.ts must do the following:

  1. main.ts: Show window + load url to Angular app
  2. Angular: does its thing
  3. Angular: uses ipcRenderer to tell main process that it's temporarily done
  4. main.ts: BrowserWindow.hide()
  5. main.ts: Does other
  6. main.ts: BrowserWindow.show()
  7. main.ts: mainWindow.webContents.send('route', '/myroute');
  8. Angular: this.ipcRenderer.on('route', (event: any, route: string) => { this.router.navigateByUrl(route); });

Expected behavior: the window hides, then is shown and shows the new component.

Actual behavior: the window hides, then is shown and shows a partially loaded component followed by the previous route's (e.g. entrypoint) component.

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Router, RouterModule, Routes } from '@angular/router';

<route component imports>

const routes: Routes = [
    { path: '', redirectTo: '/entrypoint', pathMatch: 'full' },
    { path: 'entrypoint', component: EntryPointPageComponent },
    { path: 'myroute', component: MyRouteComponent },
    { path: '**', redirectTo: '/entrypoint' }
];

@NgModule({
    exports: [ RouterModule ],
    imports: [ RouterModule.forRoot(routes, { useHash: true }) ]
})

export class AppRoutingModule {
    constructor(private router: Router) {
        this.router.events.subscribe((value) => {
            console.log(`Router event! ${ typeof value}: ${ value }`);
        }, (err) => {
            console.log(`Router failure! ${ err }`);
        });
    }
}

When it's time to go route to /myroute, I see the following events go through, in this order: NavigationStart, RoutesRecognized, GuardsCheckStart, ChildActivationStart, ActivationStart, GuardsCheckEnd, ResolveStart, ResolveEnd, ActivationEnd, ChildActivationEnd, NavigationEnd, Scroll.

Finally, I implemented all of the event lifecycles in MyRouteComponent with console log statements, but none of them get executed.

Any ideas what the issue is that's preventing routing from working successfully?

1 Answers

We were able to resolve this internally and now I feel dumb.

The trick was to wrap the routing request inside ngZone.run().

Related