首页 > 编程知识 正文

vue获取其他页面数据,vue怎么拿到后端数据

时间:2023-05-05 12:44:11 阅读:200211 作者:616

1.不跨域,携带sessionstorage打开

主页面,存储sessionstorage后,打开页面

let data = {text:'我是数据'};let isMobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)?true:false;sessionStorage.setItem('information',JSON.stringify(data));//ios不能打开新窗口,所以改为移动端在原本页面打开,pc打开新窗口window.open(window.location.protocol + "//" + window.location.host + reportUrl, isMobile?'_self':'_blank');

子页面

var date = JSON.parse(sessionStorage.getItem('information')); 2.跨域,iframe通信

跨域的情况下,无法携带sessionstorage,通过iframe的postMessage通信机制传递数据;
不跨域也可以用,但更建议使用第一种,比较丝滑~

主页面,写入url,加载完成后,发送数据

<iframe id='iframe' class="iframe" v-if="src" ref="iframe" :src="src"></iframe>let data = {text:'我是数据'};this.src = urlthis.$nextTick(()=>{document.getElementById('iframe').onload=()=>{document.getElementById('iframe').contentWindow.postMessage({type:'preview',data:data},this.src)document.getElementById('iframe').onload=null; }})

子页面,执行监听,created、mounted都可以

created() {window.addEventListener('message',(event)=>{ console.log(event.data.type) if(event.data&&event.data.type=='preview'){ console.log(event.data.data) let data = event.data.data } }, false);}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。