简体   繁体   English

Angular 材质对话框在 dialog.close() 后未关闭 function

[英]Angular Material dialog not closing after dialog.close() function

I'm struggling with this problem for a while, so maybe someone will be able to help.我在这个问题上苦苦挣扎了一段时间,所以也许有人可以提供帮助。

In my Angular project I have a dialog in my login component which opens a new popup window.在我的 Angular 项目中,我的登录组件中有一个对话框,它会打开一个新的弹出窗口 window。 After successful login this window is closing and following function is triggered:成功登录后,此 window 正在关闭并触发以下 function:

loginWindow.onbeforeunload = (e) => { 
  this.dialogRef.close()
}

And here comes the problem because dialog is still opened but I just need to click anything on the screen( even the dialog itself ) and dialog disappears.问题来了,因为对话框仍然打开,但我只需要单击屏幕上的任何内容(甚至是对话框本身),对话框就会消失。

I tried to first set focus on this window and then trigger close function but effect is still the same.我试着先把焦点放在这个 window 上然后触发关闭 function 但效果还是一样的。 Using closeAll() gives similar result.使用 closeAll() 给出类似的结果。

Thanks for any help:)谢谢你的帮助:)

EDIT:编辑:

Here's my component code:这是我的组件代码:

import { Component, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef, MatDialogConfig } from '@angular/material/dialog';

@Component({
  selector: 'app-login-page',
  templateUrl: './login-page.component.html',
  styleUrls: ['./login-page.component.scss']
})
export class LoginPageComponent implements OnInit {

  constructor(
    public dialogRef: MatDialogRef<LoginPageComponent>
  ) { }

  //some other functions

  close() {
    this.dialogRef.close();
  }

  newWindow() {
    const loginWindow = window.open(popupWindowUrl,"_blank","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
    loginWindow.onbeforeunload = (e) => { 
      this.dialogRef.close();
    }
  }

}

Can you share some code?你能分享一些代码吗?

How to you close your dialog?你如何关闭你的对话框? Are you keeping a ref to your dialog?您是否保留了对话的参考? If so can you close it with dialogRef.close();如果是这样,你可以用 dialogRef.close(); 关闭它吗?

Edit: Maybe this can help you out - What are the limitation using before unload event?编辑:也许这可以帮助你 - 卸载事件之前使用的限制是什么?

Try wrap your code inside a timeout尝试将您的代码包装在超时内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM