1. iframe与父容器通讯
// 通过postMessage与parent通讯
const params = {
methods: 'setLoginMessage',
params: {
baseInfo,
userInfo: USER
}
}
window.parent.postMessage(JSON.stringify(params), '*')
// 通过监听message事件传入回调函数处理逻辑
const connectMethods = event => {
try {
const { methods = '', params = {} } = JSON.parse(event.data || '{}')
if (methods === 'setLoginMessage') {
const { baseInfo, userInfo } = params
sessionStorage.setItem('baseInfo', JSON.stringify(baseInfo))
sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
this.$emit('success', params)
} else if (methods === 'closeLogin') {
this.$emit('closeLogin', params)
}
} catch (err) {}
}
window.addEventListener('message', connectMethods)