加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

重置Angular 2应用程序

发布时间:2020-12-17 06:56:01 所属栏目:安全 来源:网络整理
导读:我的Angular 2应用程序具有注销功能.如果可以的话,我们希望避免执行页面重新加载(即document.location.href =’/’;),但是注销过程需要重置应用程序,以便当另一个用户登录时,前一个会话中没有剩余数据. 这是我们的main.ts文件: import 'es6-shim/es6-shim';
我的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();
});

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

logout() {   
  this.destroyAuthToken();  
  this.setLoggedIn(false);
  this.navigateToLogin()
  .then(() => {
    platformBrowserDynamic().destroy();
  });
}

这会出现以下错误:

The selector “app-root” did not match any elements

任何帮助赞赏.

解决方法

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

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

启动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);
  }
}

>调整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());

>将NgZone添加到触发注销的服务:

用户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']);
    }
}

NgZone的要求是避免错误:

Expected to not be in Angular Zone,but it is!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读