diff --git a/src/views/Forward.vue b/src/views/Forward.vue index 3097ca9c..cd828c84 100644 --- a/src/views/Forward.vue +++ b/src/views/Forward.vue @@ -27,29 +27,27 @@ import portalFooter from "@/components/PortalFooter.vue"; if(params){ this.url=this.url+'?'+params; } - window.addEventListener('hashchange', this.handleHashChange); - window.addEventListener('popstate', this.handlePopState); + window.addEventListener('message', this.handleMessageFromChild); }, beforeDestroy() { - window.removeEventListener('hashchange', this.handleHashChange); - window.removeEventListener('popstate', this.handlePopState); + // 移除事件监听器 + window.removeEventListener('message', this.handleMessageFromChild); }, methods:{ - handleHashChange() { - this.navigate(); - }, - handlePopState() { - this.navigate(); - }, - navigate() { - const navigatePath = new URLSearchParams(window.location.search).get('navigate'); - if (navigatePath) { - // 清除查询参数 - window.history.replaceState({}, '', location.pathname); - // 导航 - this.$refs.iframe.src = navigatePath; + handleMessageFromChild(event) { + console.log('我进来了') + if (event.origin !== this.url) return; // 验证来源以防跨站脚本攻击 + if (event.data && event.data.type === 'navigate') { + this.navigate(event.data.path); } }, + navigate(path) { + // 清除查询参数 + // 这里假设iframe加载的是子项目,可以通过改变iframe的src属性来导航 + this.$router.push({ + path:path + }) + }, } }