《Script 引入的 Vue 项目》之已存在两个html 页面间相互跳转

首先理解需求

当我们需要从项目 index 页面,跳转到 logs 页面时:

  • 若不存在 logs 页面,则跳转到新的 logs 页面;
  • 若存在 logs 页面,则跳转到这个已打开的 logs 页面;
  • 并且当我们从 logs 页面返回 index 页面时,同理。

成果展示(主要展示两页面之间的跳转功能)

在这里插入图片描述

实现方案:

  1. window.name:为当前页面挂载一个name,当该页面在浏览器打开时,标识当前页面;
  2. window.open(url,name) :(多参数,详见:window.open() 参数
    - url:打开页面的路径;
    - name:判断当前所有已经打开的页面中的 name,有没有与 name 值相同的页面,如果有就跳转过去并刷新这个页面,没有就打开一个新页面;
1. 首页 index.html
<div id="app">
	<button class="btn" @click="linkTo('./logs.html','logs')"></button>
</div>
<script>
const app = new Vue({
	el:"#app",
	beforeCreate: function(){
		// 设置当前 window.name 为 index,其他页面使用 open() 向 index 跳转时,直接返回已创建 index 的窗体;
		window.name = 'index'
	},
	methods: {
		// url:跳转的路径
		// name:跳转目标的 name
		linkTo(url,name){
			window.open(url,name)
		}
	}
})
</script>
2. 日志 logs.html
<div id="logs">
	<button class="btn" @click="linkTo('./index.html','index')"></button>
</div>
<script>
const app = new Vue({
	el:"#logs",
	beforeCreate: function(){
		// 设置当前 window.name 为 logs,其他页面使用 open() 向 logs 跳转时,直接返回已创建 logs 的窗体;
		window.name = 'logs'
	},
	methods: {
		// url:跳转的路径
		// name:跳转目标的 name
		linkTo(url,name){
			window.open(url,name)
		}
	}
})
</script>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值