<template>
<div id="app">
<button @click="open">打开新窗口</button>
</div>
</template>
export default {
name: 'App',
data () {
this.targetWindow = null
return {
}
},
methods: {
open() {
this.targetWindow = window.open('http://localhost:8081/', '_blank', 'left=100,top=100,width=400,height=400')
// 为什么加定时器,主要是为了防止window.open()异步加载,页面没有加载出来,就把消息发送出去了,有更好的方式也可以使用其他方式
setTimeout(() => {
this.targetWindow.postMessage('旧窗口向新窗口发送的消息', 'http://localhost:8081/')
}, 1000)
}
}
}
</script>
