Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用警报消息注销用户_Angular_Typescript_Angular2 Routing - Fatal编程技术网

Angular 使用警报消息注销用户

Angular 使用警报消息注销用户,angular,typescript,angular2-routing,Angular,Typescript,Angular2 Routing,我有一个登录组件,用于控制登录页面和警报(成功、错误、信息等)。当我在页面中时,我可以毫无问题地显示警报,例如,通知用户无效凭据或通知页面正在更新。基本上,这些消息是绑定到前端的登录组件类中的变量: export class Login { error; info; } 现在,当用户注销时,我重定向到登录页面: this.router.navigate(['/login']); 我想向用户显示一个通知注销过程成功的警报(实际上我将使用相同的逻辑来通知用户会话是否过期)。我在login

我有一个登录组件,用于控制登录页面和警报(成功、错误、信息等)。当我在页面中时,我可以毫无问题地显示警报,例如,通知用户无效凭据或通知页面正在更新。基本上,这些消息是绑定到前端的登录组件类中的变量:

export class Login {
  error;
  info;
}
现在,当用户注销时,我重定向到登录页面:

this.router.navigate(['/login']);
我想向用户显示一个通知注销过程成功的警报(实际上我将使用相同的逻辑来通知用户会话是否过期)。我在login组件类中创建了两个方法:

  logout() {
    this.authenticationService.logout()
        .subscribe(response => {
            this.logoutRedirect();
        },
        error => {
            this.logoutRedirect();
        });
  }

  logoutRedirect() {
      this.authenticationService.removeUser();
      this.info = 'Logout success';
      this.router.navigate(['/login']);
  }
除了信息,一切都正常。我相信这是因为当用户被重定向时,使用了一个新的登录类,而这个类没有信息


解决这个问题的最佳方法是什么:我会在我的应用程序中大量使用它,所以我希望正确地使用它

我使用服务解决了这个问题。该服务有一个由警报组件订阅的
BehaviorSubject
。基本上:

export class AlertMessagesService {

public alertStatus: BehaviorSubject<AlertMessage>;

public addAlert(msg: string = null, type: string = 'info') {
    this.alertStatus.next({
        show: true,
        msg: msg,
        type: type
    });
}

}
导出类AlertMessagesService{
公共警报状态:行为主体;
public addAlert(msg:string=null,type:string='info'){
this.alertStatus.next({
秀:没错,
味精:味精,,
类型:类型
});
}
}
下面是显示警报的组件:

export class AlertMessages implements OnInit {

alerts: Array<AlertMessage> = [];

constructor(
    private alertService: AlertMessagesService) {
}

ngOnInit() {

    this.alertService.alertStatus.subscribe((alert: AlertMessage) => {

        if(alert.show) {
            this.alerts.push(alert);
        }

    });

}

}
导出类AlertMessages实现OnInit{
警报:数组=[];
建造师(
专用alertService:AlertMessagesService){
}
恩戈尼尼特(){
this.alertService.alertStatus.subscribe((警报:AlertMessage)=>{
if(alert.show){
此.alerts.push(警报);
}
});
}
}
我希望如果有人遇到这种情况会有所帮助