vue打开其他项目页面,并传入数据

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 = url
this.$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);
}
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜雨风亭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值