<template> <div class="hello" style="height:100%"> <div class="hader-iframe"> <div :class="logClass"> <img src="../assets/logo.png" alt /> <p>管理系統</p> ...
<template> <div class="hello" style="height:100%"> <div class="hader-iframe"> <div :class="logClass"> <img src="../assets/logo.png" alt /> <p>管理系統</p> </div> <el-menu :default-active="activeIndex2" class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" > <el-menu-item index="http://autel-cloud-web-admin-v2-dev.com/system/user">處理中心</el-menu-item> <el-menu-item index="http://autel-cloud-web-admin-dev.com/home">我的工作台</el-menu-item> <el-menu-item index="http://management-frontend-dev.com/deviceManageHome">消息中心</el-menu-item> <el-menu-item index="4">訂單管理</el-menu-item> </el-menu> </div> <div style="height:calc(100% - 61px)"> <iframe :src="url" height="100%" width="100%" name="demo" frameborder="0" scrolling="auto" sandbox="allow-same-origin allow-top-navigation allow-forms allow-scripts" ></iframe> </div> </div> </template> <script> export default { data() { return { activeIndex2: 'http://autel-cloud-web-admin-v2-dev.com/system/user', url: 'http://autel-cloud-web-admin-v2-dev.com/system/user', logClass: 'left' } }, created() {}, mounted() { window.addEventListener( 'message', e => { console.log(e) if (typeof e.data === 'boolean' && e.data) { this.logClass = 'left decrease-width' } else { this.logClass = 'left' } }, false ) }, methods: { handleSelect(key) { this.url = key } } } </script>
父級頁面向子頁面傳參
window.frames[0].postMessage(data,'http://test.com:port');//第二個參數為子頁面地址 //子頁面接參 window.addEventListener('message',function(e){ if(e.source != window.parent) return; console.log(e.data); },false);
自己頁面向父級頁面傳參
window.parent.postMessage(this.isActive, '*') //postMessage 第一個參數表示傳遞參數, 第二個參數表示域,*是所有域都能接受這個參數 //父頁面接受參數 window.addEventListener( 'message', e => { console.log(e) if (typeof e.data === 'boolean' && e.data) { this.logClass = 'left decrease-width' } else { this.logClass = 'left' } }, false )
子頁面判斷是否被 iframe 嵌套
Window.self === window.top (true 表示未被嵌套,false 表示已嵌套)