Vue进阶(贰零叁):iframe嵌套页面IE不展示问题解决

x33g5p2x  于2021-12-07 转载在 Vue.js  
字(2.8k)|赞(0)|评价(0)|浏览(375)

一、前言

在前期博文《Vue进阶(六十四):iframe更改src后页面未刷新问题解决》中讲解了iframe更改src后页面未刷新问题的解决方法,此篇博文主要讲解采用iframe方式嵌套页面IE下页面未展示问题的解决方法。

二、问题分析

项目的嵌套逻辑如下:

三、解决方法

通过B项目系统检测到A项目系统传递的系统标识,向A系统发送消息,消息体中包含A系统待展示的页面URL,A系统通过监听接收到B系统发送过来消息,刷新当前页面,处理逻辑如下:
A系统:

  1. <template>
  2. <div>
  3. <iframe v-if="hackReset" ref="otherSysIFrame" frameborder="0" height="1100" width="100%" :src="$route.params.url"></iframe>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data () {
  9. return {
  10. hackReset: false
  11. }
  12. },
  13. updated () {
  14. this.hackReset = true
  15. this.$nextTick(() => {
  16. if (this.$refs.otherSysIFrame) {
  17. let iframeSrc = this.$route.params.url
  18. if (this.getClass(iframeSrc) === 'String' && iframeSrc.indexOf(window.location.host) > -1) {
  19. this.$refs.otherSysIFrame.contentWindow.location.href = iframeSrc
  20. }
  21. }
  22. })
  23. },
  24. watch: {
  25. $route: {
  26. handler () {
  27. this.hackReset = false
  28. }
  29. }
  30. },
  31. mounted () {
  32. this.hackReset = true
  33. window.addEventListener('message', event => {
  34. if (this.$refs.otherSysIFrame) {
  35. this.$refs.otherSysIFrame.contentWindow.postMessage(event.data, '*')
  36. // IE
  37. if (this.getIEVersion() !== -1) {
  38. if (this.getClass(event.data) === 'String' && event.data.indexOf('URL_LINK') > -1) {
  39. let URL_LINK = JSON.parse(event.data).URL_LINK || ''
  40. if (URL_LINK && this.getClass(URL_LINK) === 'String') {
  41. let secondWindow = this.$refs.otherSysIFrame.contentWindow
  42. for (let i = 0; i < secondWindow.frames.length; i++) {
  43. secondWindow.frames[i].location.href = URL_LINK
  44. }
  45. }
  46. }
  47. }
  48. }
  49. })
  50. }
  51. }
  52. </script>

B系统:

  1. if (vm.$route.query.source && (vm.$route.query.source === 'castlm' || vm.$route.query.source === 'exosystem')) {
  2. vm.isExosystem = true
  3. // 外系统返回按钮显示标识
  4. if (vm.$route.query.backBtnFlag === 'backBtn') {
  5. vm.backBtnFlag = true
  6. vm.display = true // 显示返回按钮
  7. } else {
  8. vm.display = false // 隐藏返回按钮
  9. }
  10. if (vm.$route.query.bustpid === 'PROC_CCMS_SMEIRA_Main') {
  11. vm.iframeRefreshFlag = true
  12. }
  13. else if (vm.query.flag && vm.query.flag === 'Exosystem') {
  14. vm.isExosystem = true
  15. if (vm.query.bustpid) {
  16. if (vm.query.bustpid === 'PROC_CCMS_SMEIRA_Main') {
  17. vm.iframeRefreshFlag = true
  18. }
  19. vm.bustpid = vm.query.bustpid
  20. }
  21. body = {
  22. tkiids: vm.query.tkiids, // 任务实例id
  23. nodeid: vm.query.nodeid, // 当前环节
  24. tpid: vm.query.tpid, // 模板ID
  25. piids: vm.query.piids,
  26. isEdit: vm.query.isEdit // 是否可编辑页面
  27. }
  28. }
  29. ....
  30. mounted () {
  31. // console.log('mounted!')
  32. // 挂载window.onresize事件
  33. let _this = this // 复制Vue的this
  34. _this.changeFrameSize()
  35. window.onresize = () => {
  36. _this.changeFrameSize()
  37. }
  38. // 应用定时器setInterval方法用于解决OFSM双层Iframe嵌套页签不显示问题,其中URL_LINK_OFSM为获取的嵌套页面URL
  39. if ((this.isOFSM || this.iframeRefreshFlag) && document.getElementById('iframe')) {
  40. var interval = setInterval(() => {
  41. if (this.URL_LINK_OFSM) {
  42. let data = {
  43. URL_LINK: this.URL_LINK_OFSM // WFP标识
  44. }
  45. let newData = JSON.stringify(data)
  46. window.parent.postMessage(newData, '*')
  47. // 务必及时清除定时器,否则会导致浏览器崩溃
  48. clearInterval(interval)
  49. }
  50. }, 100)
  51. }
  52. // 处理任务进来后监听:用于外系统的Iframe内提交任务后返回待处理列表
  53. if (this.query.flag === 'WFP' && this.query.isEdit === '1') {
  54. window.addEventListener('message', this.listenerIframe)
  55. }
  56. },

四、拓展阅读

  • 《Vue进阶(六十四):iframe更改src后页面未刷新问题解决》

相关文章

最新文章

更多