- A页面(发送方)
const urlB = 'http://localhost:8080/caseMain' // 注意接收方的地址需要带上具体的页面路由
const otherWindowName = window.open(url);
otherWindowName.postMessage(JSON.stringify({
info: '需要传递过去的数据'
}), this.baseURL); // 接收方地址
- B页面(接收方)
作一个实时监听 //如果只监听一次,可以在获取到数据之后,作监听销毁处理
window.addEventListener('message', event => {
let urlA = 'http://192.168.0.127:8081'
if (event.origin === baseURL) {
// console.log(333);
this.caseMain = JSON.parse(event.data)
localStorage.setItem('caseMain', event.data)
// console.log(this.caseMain, localStorage.getItem('caseMain'),'11111');
}
}, false);