本方案采用广播形式,实现跨标签广播。示例为退出广播监听
新建src\utils\broadcastChannel.js
const BroadCast = new BroadcastChannel('com.project.yunshu.broadcast')
export default BroadCast
在main.js中引入
import broadcast from '@/utils/broadcastChannel'
// 保存整站本地储存广播
window.broadcast = broadcast
// 添加广播监听
broadcast.onmessage = function (res) {
if ('userLogout' === res.data) {
clearLoginInfo()
router.push({ name: 'login' })
broadcast.close()
}
}
// 在退出函数中调用广播发送
logout () {
...
window.broadcast.postMessage('userLogout')
}