window

作者: 阿凯_8b27 | 来源:发表于2019-10-14 11:18 被阅读0次

业务场景:  当前窗口打开子窗口,当子窗口关闭时候,父窗口刷新页面

窗口之间的通信方式

1. 父窗口启动计时器轮训子窗口是否关闭
f (this.window_Report == null) {

           let window_A = window.open('https://www.jianshu.com/writer#/notebooks/38692919/notes/53628453',

                'a');

            this.timer = setInterval(() => {

                //父窗口去检测子窗口是否关闭,然后通过自我刷新,而不是子窗口去刷新父窗口

                if (this.window_A .closed == true) {

                    clearInterval(this.timer);

                    this.window_A = null

                    return;

                }

            }, 1000);

        } else {

            this.window_A .window.focus();

        }

2.子窗口调用父窗口页面中页面元素的click事件
    父窗口写

<div  id="get" (click)="close()"></div>

子窗口写

window.opener.document.getElementById('get').click();

3.子窗口给父窗口发送postMeaage

子窗口给
window.opener.postMessage({ event: 'changeRole' }, '*')

父窗口

window.addEventListener('message', e => {

                if (e.data && e.data.event == 'changeRole'){

                    window.close();

                }

            })

        },

相关文章

网友评论

      本文标题:window

      本文链接:https://www.haomeiwen.com/subject/gtkdyctx.html