My Angular 2 app has a logout feature. We want to avoid doing a page reload if we can (i.e. document.location.href = '/';
), but the logout process needs to reset the app so when another user logs in there's no residual data from the previous session.
Here's our main.ts file:
import 'es6-shim/es6-shim';
import './polyfills';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { ComponentRef, enableProdMode } from '@angular/core';
import { environment } from '@environment';
import { AppModule } from './app/app.module';
if (environment.production === true) {
enableProdMode();
}
const init = () => {
platformBrowserDynamic().bootstrapModule(AppModule)
.then(() => (<any>window).appBootstrap && (<any>window).appBootstrap())
.catch(err => console.error(err));
};
init();
platformBrowserDynamic().onDestroy(() => {
init();
});
You can see that I'm trying to call the init() method when the application is destroyed. The logout method in our user-authentication.service initiates destroy:
logout() {
this.destroyAuthToken();
this.setLoggedIn(false);
this.navigateToLogin()
.then(() => {
platformBrowserDynamic().destroy();
});
}
This gives the following error:
The selector "app-root" did not match any elements
Any help appreciated.