英文:
How to properly add an event listener to a child window in vuejs?
问题
从Vue 3组件中,我需要一个新窗口,就像一个弹出窗口,使用 window.open(url, name, config)
。并且当子窗口关闭时,我需要触发一个函数。
(请注意,这与子父组件或相反的事件无关,而是涉及到窗口对象及其父窗口)
我尝试在弹出窗口上添加事件监听器,但这些事件在子窗口重定向时被销毁。
在组件的 script 标签中,我添加了一个方法,用于在按钮点击时打开新窗口并进行其他处理,如下所示:
<template>
....
<button @click="onPositive"></button>
</template>
<script lang="ts">
export default defineComponent({
methods: {
onPositive() {
const popup = window.open(url, name, config) as Window
popup.focus()
// 方法 1:
popup.addEventListener('beforeunload', () => {
console.log("事件触发")
this.onPopupClosed
})
// 方法 2:
popup.onbeforeunload = () => {
console.log("事件触发")
this.onPopupClosed
}
},
onPopupClosed() {
// .....
}
}
})
</script>
我尝试添加各种事件监听器,像上面提到的2种方法,但仍然没有触发。
一些见解:
- url 来自不同的源。
- 在url被重定向之前,当我关闭子窗口时,它会触发。
- 使用Vue.js 3和Typescript 5。
英文:
From a component in vue 3, i needed a new window, like a popup, using window.open(url, name, config)
. And i need to trigger a function when the child window is closed.
(Please note that this is not regarding events on child-parent component, or vice-versa, rather on a window object and its parent)
I tried adding event listeners on the popup window, but those are getting destroyed when the url is redirected on the child window.
Inside the script tab on a component, added a method to open new window and do other processing, on a button click, like below:
<template>
....
<button @click="onPositive"></button>
</template>
<script lang="ts">
export default defineComponent({
methods: {
onPositive() {
const popup = window.open(url, name, config) as Window
popup.focus()
// method 1:
popup.addEventListener('beforeunload' () => {
console.log("event triggered)
this.onPopupClosed
})
// method 2:
popup.onbeforeunload = () => {
console.log("event triggered)
this.onPopupClosed
}
},
onPopupClosed() {
// .....
}
}
})
</script>
I tried adding various event listeners like the 2 methods mentioned above, but still its not getting triggered.
Some insights:
- url is of different origin.
- Before the url is getting redirected, when i close the child window, its getting triggered.
- Using Vue.js 3 & typescript 5.
答案1
得分: 0
如果弹出窗口在您的控制下,您可以像这样做。
在最后一个地方,当所有导航都完成时
<script>
window.addEventListener('beforeunload', () => {
window.opener.postMessage('popup-closed', '*');
});
</script>
window.opener.postMessage()
是一种允许当前窗口(子窗口)与打开它的窗口(父窗口)进行通信的方法。它提供了一种在这两个窗口之间发送消息的方式,即使它们来自不同的源(即不同的域、协议或端口)也可以。
在您的父Vue组件中
...
// 监听来自子窗口的消息
window.addEventListener('message', (event) => {
if (event.data === 'popup-closed') {
onPopupClosed();
}
});
const onPopupClosed = () => {
// 处理子窗口关闭时的事件
console.log('弹出窗口已关闭');
};
window.addEventListener('message', ...)
是JavaScript中的事件监听器,允许您监听来自网页中其他窗口或iframe发送的消息。它使不同窗口或iframe之间的跨源通信成为可能,无论它们在同一页面上还是跨不同的域、协议或端口。
还有一点是,window.opener
属性始终指向打开子窗口的父窗口,无论在最终目标之前是否经过了其他URL的中间导航。一旦使用 window.open()
从父窗口打开子窗口,父窗口的引用将在子窗口的生命周期内保持不变,即使子窗口在最终目标之前导航到其他URL。
英文:
If the popup is under your control, you can do something like this.
In the final place, when all the navigation is done
<script>
window.addEventListener('beforeunload', () => {
window.opener.postMessage('popup-closed', '*');
});
</script>
window.opener.postMessage()
is a method that allows communication between the current window (child window) and the window that opened it (parent window). It provides a way to send messages between the two windows, even if they are from different origins (i.e., different domains, protocols, or ports).
In your parent vue component
...
// Listen for messages from the child window
window.addEventListener('message', (event) => {
if (event.data === 'popup-closed') {
onPopupClosed();
}
});
const onPopupClosed = () => {
// Handle the event when the child window is closed
console.log('Popup closed');
};
window.addEventListener('message', ...)
is an event listener in JavaScript that allows you to listen for messages sent from other windows or iframes in a web page. It enables cross-origin communication between different windows or iframes on the same page or across different domains, protocols, or ports.
One more thing is the window.opener
property always refers to the parent window that opened the child window, regardless of any intermediate navigation to other URLs. Once a child window is opened using window.open()
from the parent window, the reference to the parent window remains the same throughout the child window's lifetime, even if the child window navigates to other URLs before go through final destination.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论