This commit is contained in:
zhangsir
2024-05-28 18:07:06 +08:00
parent 7df4643c60
commit a98a63c014

View File

@@ -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
})
},
}
}
</script>