{"id":8123,"date":"2023-11-28T20:14:57","date_gmt":"2023-11-28T12:14:57","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8123"},"modified":"2023-12-02T21:28:53","modified_gmt":"2023-12-02T13:28:53","slug":"vue-zhonghash-lu-you-hehistory-lu-you-shi-xian-yua","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/vue-zhonghash-lu-you-hehistory-lu-you-shi-xian-yua\/","title":{"rendered":"Vue \u4e2d hash \u8def\u7531\u548c history \u8def\u7531\u5b9e\u73b0\u539f\u7406\u8bf4\u4e00\u4e0b\uff1f"},"content":{"rendered":"<p>\u5728 Vue \u4e2d\uff0cHash \u8def\u7531\u548c History \u8def\u7531\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u8def\u7531\u6a21\u5f0f\uff0c\u5b83\u4eec\u7684\u5b9e\u73b0\u539f\u7406\u548c\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u8868\u73b0\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<h3><a id=\"hash%E8%B7%AF%E7%94%B1%EF%BC%88-hash-mode%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Hash \u8def\u7531\uff08Hash mode\uff09\uff1a<\/h3>\n<ul>\n<li>Hash \u8def\u7531\u4f7f\u7528 URL \u4e2d\u7684 <code>#<\/code> \u7b26\u53f7\u6765\u6a21\u62df\u8def\u7531\uff0c\u4f8b\u5982\uff1a<code>http:\/\/example.com\/#\/about<\/code>\u3002<\/li>\n<li>\u5b9e\u73b0\u539f\u7406\u662f\u76d1\u542c\u6d4f\u89c8\u5668\u7684 <code>hashchange<\/code> \u4e8b\u4ef6\uff0c\u5728 URL \u4e2d\u7684\u54c8\u5e0c\u503c\u6539\u53d8\u65f6\uff0c\u6839\u636e\u65b0\u7684\u54c8\u5e0c\u503c\u6765\u5207\u6362\u8def\u7531\u3002<\/li>\n<li>\u4f18\u70b9\u662f\u517c\u5bb9\u6027\u597d\uff0c\u53ef\u4ee5\u5728\u4e0d\u652f\u6301 History API \u7684\u6d4f\u89c8\u5668\u4e0a\u8fd0\u884c\uff0c\u4e14\u90e8\u7f72\u7b80\u5355\u3002<\/li>\n<li>\u7f3a\u70b9\u662f URL \u4e2d\u6709 <code>#<\/code> \u7b26\u53f7\uff0c\u4e0d\u591f\u7f8e\u89c2\uff0c\u540c\u65f6\u65e0\u6cd5\u76f4\u63a5\u5b9a\u4f4d\u5230\u9875\u9762\u4e2d\u7684\u951a\u70b9\u3002<\/li>\n<\/ul>\n<h3><a id=\"history%E8%B7%AF%E7%94%B1%EF%BC%88-history-mode%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>History \u8def\u7531\uff08History mode\uff09\uff1a<\/h3>\n<ul>\n<li>History \u8def\u7531\u5229\u7528 HTML5 \u7684 History API\uff0c\u901a\u8fc7\u4fee\u6539\u6d4f\u89c8\u5668\u7684 History Stack \u6765\u5b9e\u73b0\u8def\u7531\u5207\u6362\uff0c\u4f8b\u5982\uff1a<code>http:\/\/example.com\/about<\/code>\u3002<\/li>\n<li>\u5b9e\u73b0\u539f\u7406\u662f\u4f7f\u7528 <code>history.pushState()<\/code> \u65b9\u6cd5\u6216 <code>history.replaceState()<\/code> \u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u6d4f\u89c8\u5668\u7684 URL\u3002<\/li>\n<li>\u4f18\u70b9\u662f URL \u66f4\u52a0\u7f8e\u89c2\uff0c\u6ca1\u6709 <code>#<\/code> \u7b26\u53f7\uff0c\u66f4\u7b26\u5408\u4f20\u7edf URL \u7684\u5f62\u5f0f\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528 <code>history.back()<\/code>\u3001<code>history.forward()<\/code> \u7b49\u65b9\u6cd5\u63a7\u5236\u8def\u7531\u5386\u53f2\u3002<\/li>\n<li>\u7f3a\u70b9\u662f\u9700\u8981\u670d\u52a1\u5668\u7684\u914d\u5408\uff0c\u4ee5\u4fbf\u5728\u975e\u6839\u8def\u5f84\u4e0b\u5237\u65b0\u9875\u9762\u65f6\u6b63\u786e\u5730\u5b9a\u4f4d\u5230\u5bf9\u5e94\u7684\u8def\u7531\u3002<\/li>\n<\/ul>\n<p>\u5728 Vue \u4e2d\uff0c\u901a\u8fc7 <code>vue-router<\/code> \u6765\u4f7f\u7528\u8fd9\u4e24\u79cd\u8def\u7531\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u8def\u7531\u5668\u914d\u7f6e\u4e2d\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u6a21\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">const router = new VueRouter({\n  mode: 'history', \/\/ \u6216 'hash'\n  routes: [\n    \/\/ \u8def\u7531\u914d\u7f6e\n  ]\n})\n<\/code><\/pre>\n<p>\u9009\u62e9\u4f7f\u7528 Hash \u8def\u7531\u8fd8\u662f History \u8def\u7531\uff0c\u53d6\u51b3\u4e8e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u548c\u5bf9\u7f8e\u89c2\u6027\u3001\u517c\u5bb9\u6027\u7684\u8003\u91cf\u3002 History \u8def\u7531\u76f8\u5bf9\u66f4\u4f18\u96c5\uff0c\u4f46\u9700\u8981\u670d\u52a1\u5668\u7684\u914d\u5408\u652f\u6301\uff0c\u5e76\u4e14\u5728\u4e00\u4e9b\u65e7\u7248\u672c\u6d4f\u89c8\u5668\u4e2d\u53ef\u80fd\u4e0d\u517c\u5bb9\u3002Hash \u8def\u7531\u517c\u5bb9\u6027\u66f4\u597d\uff0c\u90e8\u7f72\u7b80\u5355\uff0c\u4f46 URL \u4e0d\u591f\u7f8e\u89c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Vue \u4e2d\uff0cHash \u8def\u7531\u548c History \u8def\u7531\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u8def\u7531\u6a21\u5f0f\uff0c\u5b83\u4eec\u7684\u5b9e\u73b0\u539f\u7406\u548c\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u8868\u73b0\u6709 [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-8123","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8123"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8123\/revisions"}],"predecessor-version":[{"id":14038,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8123\/revisions\/14038"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8123"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}