重置Angular 2应用程序

ben*_*h80 8 angular2-services angular

我的Angular 2应用程序具有注销功能.如果我们可以(即document.location.href = '/';),我们希望避免执行页面重新加载,但是注销过程需要重置应用程序,以便当另一个用户登录时,前一个会话中没有剩余数据.

这是我们的main.ts文件:

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();
});
Run Code Online (Sandbox Code Playgroud)

您可以看到我在销毁应用程序时尝试调用init()方法.我们的user-authentication.service中的logout方法启动destroy:

logout() {   
  this.destroyAuthToken();  
  this.setLoggedIn(false);
  this.navigateToLogin()
  .then(() => {
    platformBrowserDynamic().destroy();
  });
}
Run Code Online (Sandbox Code Playgroud)

这会出现以下错误:

选择器"app-root"与任何元素都不匹配

任何帮助赞赏.

ben*_*h80 22

我最终搞清楚了.这可以比我的实现更简单地完成,但我想保持引导main.ts而不是将其粘贴在请求重启的服务中.

  1. 创建一个单例,为Angular和非Angular(main.ts)提供通信方式:

boot-control.ts:

import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
export class BootController {
  private static instance: BootController;
  private _reboot: Subject<boolean> = new Subject();
  private reboot$ = this._reboot.asObservable();

  static getbootControl() {
    if (!BootController.instance) {
      BootController.instance = new BootController();
    }
    return BootController.instance;
  }

  public watchReboot() {
    return this.reboot$;
  }

  public restart() {
    this._reboot.next(true);
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 调整main.ts以订阅重新启动请求:

main.ts:

import { enableProdMode, NgModuleRef, NgModule } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { BootController } from './boot-control';

if (environment.production) {
  enableProdMode();
}

const init = () => {
  platformBrowserDynamic().bootstrapModule(AppModule)
  .then(() => (<any>window).appBootstrap && (<any>window).appBootstrap())
  .catch(err => console.error('NG Bootstrap Error =>', err));
}

// Init on first load
init();

// Init on reboot request
const boot = BootController.getbootControl().watchReboot().subscribe(() => init());
Run Code Online (Sandbox Code Playgroud)
  1. 将NgZone添加到触发注销的服务:

user-auth.service.ts:

import { BootController } from '@app/../boot-control';
import { Injectable, NgZone } from '@angular/core';

@Injectable()
export class UserAuthenticationService {
    constructor (
        private ngZone: NgZone,
        private router: Router
    ) {...}

    logout() {
        // Removes auth token kept in local storage (not strictly relevant to this demo)
        this.removeAuthToken();

        // Triggers the reboot in main.ts        
        this.ngZone.runOutsideAngular(() => BootController.getbootControl().restart());

        // Navigate back to login
        this.router.navigate(['login']);
    }
}
Run Code Online (Sandbox Code Playgroud)

NgZone的要求是避免错误:

预计不会在Angular Zone,但它是!

  • 这种方法有一个警告,每次应用程序重新启动时,所有 CSS 样式都会被克隆并附加到 HEAD 标签。这将导致应用程序出现一些奇怪的行为,至少在我的例子中是这样。 (2认同)

小智 5

我遇到了同样的问题。一种更简单的方法是使用location.reload()

用户单击注销按钮时调用的App.component中的函数应如下所示。

logout() {
  //Auth Logout service call
  this.auth.logout();
  //Router Navigation to Login Page
  this.router.navigate(['login']);
  //Reload Angular to refresh components and prevent old data from loading up for a 
  //another user after login. This especially applies lazy loading cases. 
  location.reload(); 
}
Run Code Online (Sandbox Code Playgroud)