当前位置:网站首页>关于hash和history的区别和使用
关于hash和history的区别和使用
2022-07-15 12:58:00 【cc.ChenLy】
一、区别
1、 history和hash都是利用浏览器的两种特性实现前端路由,history是利用浏览历史记录栈的API实现,hash是监听location对象hash值变化事件来实现
2、 history的url没有’#'号,hash反之
3、 相同的url,history会触发添加到浏览器历史记录栈中,hash不会触发,history需要后端配合,如果后端不配合刷新新页面会出现404,hash不需要
4、
HashRouter的原理:通过window.onhashchange方法获取新URL中hash值,再做进一步处理 HistoryRouter的原理:通过history.pushState 使用它做页面跳转不会触发页面刷新,使用window.onpopstate 监听浏览器的前进和后退,再做其他处理
二、使用
举例说明history模式
1、现有一个这样的页面(Vue2)

2、设置路由模式

3、打包
使用webpack的npm build命令,生成以下文件
4、模拟一个服务器(nodejs的express)
初始化

调试

5、把打包好的文件放到服务器
6、访问,路由跳转不涉及网络请求
7、刷新页面,资源请求失败,发送网络请求/MyHome/message/details/1/message001失败,因为服务器上没有这个请求(history模式存在这个问题,hash模式则无)
8、解决办法
使用connect-history,根据npm官方文档

9、再次调试,刷新后依旧没问题
边栏推荐
- AI briefing - model integration Sam and SWA
- Can all three nodes of polardb for PostgreSQL read and write?
- Alibaba cloud official document "100% compatible with MySQL and postgre SQL" refers to two kinds of databases, right?
- Is polardb for PostgreSQL completely open source?
- 360预计上半年扣非净亏4.5亿至6.3亿 广告投放预算不及预期
- ZCMU--1099: 查找元素II
- 难道双非本科就一定进不了大厂?阿里技术四面+交叉面+HR面,成功拿到offer!!
- What is the overall solution for Oracle to migrate to polardb for PostgreSQL?
- Seize the new track, and the number of groups vigorously layout the "meta universe" industry
- Where is the IBD file of MySQL in win11 virtual machine
猜你喜欢

torch.nn.CTCLoss()的使用

【图表组件套件开发】上海道宁为开发人员提供Steema下载、试用、教程

Hydropower station equipment can also be operated remotely

torch. nn. Use of ctcloss()

Do you know the architecture and engine of MySQL?

McKinsey: in the next decade, the top ten technology trends will affect investment and research direction

MES管理系统的四个功能,高效提升工厂数字化

Flowable 查询当前用户的待办任务方法报错

Trends in Plant Science | 向改善生态的植物-微生物组互作的方向育种

Teach people to fish - see a field on the sap mm material display interface, how to find which field of which database table to store
随机推荐
让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北
Andorid studio makes happy writing numbers (timer start + frame animation)
Issue 105: a failed browser plug-in development
[Vuforia] 详解·高通Vuforia识别追踪3D物体/模型,Unity开发
Implementation of ZABBIX proxy active mode
PKI: content of digital certificate
The latest research of Zhu Songchun's team: robots can "confide" with humans! Also said that the next step is to create "Ai white"
Google Earth engine (GEE) -- abnormal value of S2 image
四面阿里offer定级P8,2022最新最实用阿里68道高级面试题,助你们面试成功!!
Thesis learning (I) -- MWP bert: numerical augmented pre training for math wordproblem solving
腾讯T4架构师带你“一窥”大型网站架构的主要技术挑战和解决方案
[vuforia] detailed explanation · Qualcomm vuforia identifies and tracks 3D objects / models, developed by unity
Unp learning notes - Chapter 2 transport layer
探索智能驾驶区间测速NTP时钟同步(PTP时间同步)
JGG (if 5.733) special issue solicitation: Human Microbiome
MGRE综合实验
Event preview | Apache Doris x Apache seatunnel joint meetup to start registration!
Chromium Threading and Task
AI briefing - model integration Sam and SWA
MGRE与OSPF