一、Main Page

1、created()中添加事件监测(从子页面发送过来的消息)

window.addEventListener('message', this.iframeEvent)

 

2、mounte()中获取id名为iframe的对象:

this.iframe = this.$refs.iframe

this.iframeWindow = this.iframe.contentWindow

 

3、methods中定义事件回调方法:

iframeEvent (event) {

    if (event.data.cmd === -1) {

        this.$message({showClose: true, message: '登录超时,请重新登录!', type: 'warning'})

        removeToken()

        this.$router.push({name: '/login'})

    }

}

 

4、向子页面发送消息

document.getElementById('iframe').contentWindow.postMessage(‘message’, '*')

二、Sub Page

1、created()中添加事件监测(从父页面发送过来的消息)

window.onload = function () {

    window.addEventListener('message', function (e) {

    if (e.data.hasOwnProperty('token')) {

          setToken(e.data.token)

     }

})

 

2、向父页面发送消息

window.parent.postMessage('message', '*')

Logo

前往低代码交流专区

更多推荐