Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
3
Star
47
Fork
23
DreamCoders
/
CoderGuide
Code
Issues
1169
Pull Requests
0
Wiki
Insights
Pipelines
Service
JavaDoc
PHPDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
DevLens
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
vue中hash和history的区别 ?
Backlog
#IAGA5G
陌生人
owner
Opened this issue
2024-07-29 16:19
<p>Vue-router的路由分为hash和history模式</p><h4>1、hash方式</h4><p>hash方式是指url中存在 # 的一种方式,是vueRouter的默认模式,</p><p>当#后面的url地址发生变化时,浏览器不会向服务器发送请求,故不会刷新页面</p><p>当#后面的url地址发生变化时,会触发hashChange(hash模式得核心实现原理)事件,从而,我们可以通过监听hashChange事件来知道路由发生变化,从而进一步去更新我们的页面</p><p>只可修改hash部分,</p><p>当浏览器刷新时,浏览器只会向服务器去请求# 前面的域名服务器下根目录下的index.html文件</p><p>hash模式会创建hashHistory对象,hashHistory对象有两个方法,push() 和 replace()</p><p>HashHistory.push()会将新的路由添加到浏览器访问的历史的栈顶,而HasHistory.replace()会替换到当前栈顶的路由</p><h4>2、history模式</h4><p>history模式得路由和域名之间直接通过/连接,无#符分隔,就是普通url形式</p><p>history模式当发生路由跳转时,通过HTML5的history.pushState()方法或者history.replaceState() 方法改变地址栏地址,并将地址的改变记录到浏览器访问栈中。(这里有一点需要注意,它只改变了浏览器地址栏中的地址,但并不会像服务器去发送请求)</p><p>当浏览器前进,后台,或者调用back(),forward(), go()等方法时,会触发popstate事件。故,我们可以通过监听popstate事件来获取最新的路由地址,从而更新页面</p><p>通过pushstate() 修改的url可以是与当前url同源的任意url。</p><p>需要和服务器配合使用,否则容易出现页面404的情况</p><p><br></p><h4>总结如下:</h4><p>hash模式带#号比较丑,history模式比较优雅;</p><p>pushState设置的新的URL可以是与当前URL同源的任意URL;而hash只可修改#后面的部分,故只可设置与当前同文档的URL;</p><p>pushState设置的新URL可以与当前URL一模一样,这样也会把记录添加到栈中;而hash设置的新值必须与原来不一样才会触发记录添加到栈中;</p><p>pushState通过stateObject可以添加任意类型的数据到记录中;而hash只可添加短字符串;</p><p>pushState可额外设置title属性供后续使用;</p><p>hash兼容IE8以上,history兼容IE10以上;</p><p>history模式需要后端配合将所有访问都指向index.html,否则用户刷新页面,会导致404错误。</p>
<p>Vue-router的路由分为hash和history模式</p><h4>1、hash方式</h4><p>hash方式是指url中存在 # 的一种方式,是vueRouter的默认模式,</p><p>当#后面的url地址发生变化时,浏览器不会向服务器发送请求,故不会刷新页面</p><p>当#后面的url地址发生变化时,会触发hashChange(hash模式得核心实现原理)事件,从而,我们可以通过监听hashChange事件来知道路由发生变化,从而进一步去更新我们的页面</p><p>只可修改hash部分,</p><p>当浏览器刷新时,浏览器只会向服务器去请求# 前面的域名服务器下根目录下的index.html文件</p><p>hash模式会创建hashHistory对象,hashHistory对象有两个方法,push() 和 replace()</p><p>HashHistory.push()会将新的路由添加到浏览器访问的历史的栈顶,而HasHistory.replace()会替换到当前栈顶的路由</p><h4>2、history模式</h4><p>history模式得路由和域名之间直接通过/连接,无#符分隔,就是普通url形式</p><p>history模式当发生路由跳转时,通过HTML5的history.pushState()方法或者history.replaceState() 方法改变地址栏地址,并将地址的改变记录到浏览器访问栈中。(这里有一点需要注意,它只改变了浏览器地址栏中的地址,但并不会像服务器去发送请求)</p><p>当浏览器前进,后台,或者调用back(),forward(), go()等方法时,会触发popstate事件。故,我们可以通过监听popstate事件来获取最新的路由地址,从而更新页面</p><p>通过pushstate() 修改的url可以是与当前url同源的任意url。</p><p>需要和服务器配合使用,否则容易出现页面404的情况</p><p><br></p><h4>总结如下:</h4><p>hash模式带#号比较丑,history模式比较优雅;</p><p>pushState设置的新的URL可以是与当前URL同源的任意URL;而hash只可修改#后面的部分,故只可设置与当前同文档的URL;</p><p>pushState设置的新URL可以与当前URL一模一样,这样也会把记录添加到栈中;而hash设置的新值必须与原来不一样才会触发记录添加到栈中;</p><p>pushState通过stateObject可以添加任意类型的数据到记录中;而hash只可添加短字符串;</p><p>pushState可额外设置title属性供后续使用;</p><p>hash兼容IE8以上,history兼容IE10以上;</p><p>history模式需要后端配合将所有访问都指向index.html,否则用户刷新页面,会导致404错误。</p>
Comments (
0
)
Sign in
to comment
Status
Backlog
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
Vue
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
Branches (
-
)
Tags (
-
)
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register