【问题标题】:How to close the router opened in new tab on logout in angular8如何关闭在角度 8 中注销时在新选项卡中打开的路由器
【发布时间】:2020-06-02 19:25:40
【问题描述】:

您好,我已使用锚标记在新选项卡中打开 routerLink。现在,如果我单击注销,新选项卡中的 routerLink 保持不变,如果有任何 API 命中,则 routerLink 重定向到登录页面。但我希望在我注销后立即关闭所有选项卡,因为其他路由器页面在屏幕上没有标题。

Header.component.ts:

public logout() {
    this.authservice.logOutSuccess(true);
    this.router.navigate(["/login"]);
    this.cookieService.delete(TPIConstants.AUTH_TOKEN);
  }

在标题组件中,如果我点击注销,我将清除 cookie,因此屏幕将重定向到登录页面。

但是在登陆页面中,我添加了新按钮,当我点击它时,它会在新选项卡中打开路由器,

Landing.html:

 <a class="btn btn-light" target="_blank" [routerLink]="['/profile']"> Add New</a>

个人资料屏幕现在在新标签中打开。

所以,我使用了一个名为 auth.service 的通用服务,其中我使用了主题

auth.service.ts:

 public  detectLogOutSubject = new Subject<any>();
  logOutConfirmed$ = this.detectLogOutSubject.asObservable();

 logOutSuccess(value) {
    this.detectLogOutSubject.next(value);
  }

所以当我点击注销时,它会来到 authservice 并触发此事件,并且在配置文件页面中我在构造函数方法中这样写

profile.component.ts: 和 app.component.ts

constructor(){
    this.subscription = this.authService.logOutConfirmed$.subscribe(
      res => {
        alert(res)
    });
   }

个人资料.html:

<div>
                <button class="btn btn-secondary" onclick="window.close();" title="Close Window">
                    <i class="fas fa-times fa-1x"></i>
                </button>
            </div>

在配置文件 html 中,我有上面的 div 部分,如果该点击也被触发,它将关闭选项卡,但我也无法从注销功能获取事件到配置文件屏幕/app.component,我很震惊, 不知道怎么做

【问题讨论】:

    标签: javascript angular


    【解决方案1】:

    因为您没有使用 window.open() 打开新标签,您的 Javascript 将无权关闭标签。如果关闭选项卡很重要,那么您需要使用 window.open() 打开选项卡,然后您可以在准备好时使用 window.close() 关闭它。

    更多关于安全和权限的讨论可以found here

    【讨论】:

    • 感谢您的回复,但是当我使用 window.open 时,我在部署时遇到了问题,所以我改为使用 routerLink。并且这里没有检测到事件,因为屏幕正在新标签中打开
    • 有没有更好的方法来处理这个问题?
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2012-08-19
    • 2017-04-08
    • 2016-02-14
    相关资源
    最近更新 更多