vue的俩种路由模式hash和history

本文深入探讨了前端路由中Hash模式与History模式的区别。Hash模式使用#号进行路由跳转,而History模式则通过HTML5的history API实现无刷新页面状态变更。通过实例展示了两种模式的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hash模式 是默认的,就是在地址栏 访问上面有一个 #号
http://localhost:8081/#/home,http://localhost:8081/#/search
hash模式下 默认的地址栏 默认不会讲# 号 后面的发送给服务器端
在发送之前 前端会判断下 将# 号后面的过滤掉
所以上面的就是 http://localhost:8081/

history模式 则是利用html5中新增的 history.pushState({},’’,url);
就是更改地址栏的状态但是页面并不会发生变化。
这个我来举一个例子吧,看完例子应该就会懂了
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>history原理</title>
</head>
<body>
    <h1>history的演示</h1>
	<p id="msg"></p>
    <button id="home">跳转到首页页面</button>
    <button id="friend">跳转到好友页面</button>
    <script>
        home.onclick=function(){
			history.pushState({},'','/home');
			msg.innerHTML="我是首页页面";
		}
		friend.onclick=function(){
			history.pushState({},'','/friend');
			msg.innerHTML="我是好友页面";
		}
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yunchong_zhao

帮到你了,请作者喝杯矿泉水可好

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

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

打赏作者

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

抵扣说明:

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

余额充值