{"id":3350,"date":"2022-08-06T23:17:33","date_gmt":"2022-08-06T15:17:33","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=3350"},"modified":"2022-08-06T23:23:42","modified_gmt":"2022-08-06T15:23:42","slug":"hash%e8%b7%af%e7%94%b1%e5%92%8chistory%e8%b7%af%e7%94%b1%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=3350","title":{"rendered":"hash\u8def\u7531\u548chistory\u8def\u7531\u539f\u7406"},"content":{"rendered":"<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/08\/Snipaste_2022-08-06_23-21-16.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3360\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/08\/Snipaste_2022-08-06_23-21-16.png\" alt=\"Snipaste_2022-08-06_23-21-16\" width=\"672\" height=\"344\" \/><\/a><\/p>\n<p>\u9762\u8bd5vue\u5f00\u53d1\u65f6\uff0c\u9762\u8bd5\u5b98\u7ecf\u5e38\u95ee:<\/p>\n<ol>\n<li>\u8bf7\u8bf4\u4e0b vue\u6846\u67b6\u8def\u7531\u6a21\u5f0f\u6709\u90a3\u4e24\u79cd.\u4f60\u80af\u5b9a\u4f1a\u6beb\u4e0d\u72b9\u8c6b\u7b54\u51fa: hash\u8def\u7531\u548chistory\u8def\u7531<\/li>\n<li>\u63a5\u7740\u53ef\u80fd\u4f1a\u95ee\uff0c\u5b83\u4eec\u4e24\u8005\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b?\u4f60\u81ea\u4fe1\u7684\u56de\u7b54\u5230\uff1a<\/li>\n<\/ol>\n<p>hash\u8def\u7531\u662f\u5229\u7528hash\u503c\u5b9e\u73b0\u754c\u9762\u5207\u6362.<\/p>\n<p>hash\u503c\u662furl\u5730\u5740#\u53f7\u540e\u9762\u7684\u5185\u5bb9, \u53ef\u4ee5\u4f5c\u4e3a\u9875\u9762\u7684\u951a\u70b9\uff0c\u5b9e\u73b0\u4e00\u4e2a\u9875\u9762\u4e0d\u540c\u951a\u70b9\u4e4b\u95f4\u7684\u8df3\u8f6c.<\/p>\n<p>\u4f8b\u5982: \u5f53\u4ecehash\u503c\u4e3a#a\u7684\u8def\u5f84\u5207\u6362\u5230hash\u503c\u4e3a#b\u7684\u8def\u5f84\u65f6\uff0c\u754c\u9762\u4f1a\u4ecea\u5207\u6362\u5230b;<\/p>\n<p>history\u8def\u7531\u662f\u5229\u7528history\u5bf9\u8c61putState\u65b9\u6cd5\u6216replaceState\u65b9\u6cd5\u5b58\u50a8\u9875\u9762\u8def\u5f84\u5b8c\u6210\u5207\u6362<\/p>\n<p>history\u5bf9\u8c61\u662f\u6d4f\u89c8\u5668\u5386\u53f2\u8bb0\u5f55\u5bf9\u8c61<\/p>\n<p>3. \u5927\u90e8\u4efd\u540c\u5b66\u5230\u8fd9\u91cc\u53ef\u80fd\u5c31\u7ed3\u675f\u4e86\uff0c\u4f46\u9762\u8bd5\u5b98\u63a5\u7740\u8ffd\u95ee\uff0c\u80fd\u8be6\u7ec6\u8bf4\u4e0b\u5b83\u4eec\u7684\u5b9e\u73b0\u539f\u7406\u5417\uff1f\u6216\u8005\u8ba9\u4f60\u624b\u52a8\u5b9e\u73b0vue\u8def\u7531\u8be5\u5982\u4f55\u5b9e\u73b0?\u5b9e\u9645\u6ca1\u60f3\u50cf\u90a3\u4e48\u590d\u6742\uff0c\u6211\u4eec\u6765\u770b\u770b\u5b9e\u73b0\u7c7b\u4f3c\u4e8evue\u6846\u67b6hash\u8def\u7531:<\/p>\n<ul>\n<li>\u521b\u5efa index.html\u9875\u9762, \u770b\u6ce8\u91ca\u5e94\u8be5\u80fd\u770b\u61c2, \u4e0e vue \u8def\u7531router-link\u7ec4\u4ef6\u548crouter-view\u7ec4\u4ef6\u4f5c\u7528\u76f8\u5f53\n<pre><code class=\"language-html\" lang=\"html\">&lt;body&gt;\r\n    &lt;!-- \u76f8\u5f53\u4e8e router-link --&gt;\r\n    &lt;a href=\".\/#home\"&gt;\u9996\u9875&lt;\/a&gt; | &lt;a href=\".\/#about\"&gt;\u5173\u4e8e\u6211\u4eec&lt;\/a&gt; | &lt;a href=\".\/#introduce\"&gt;\u516c\u53f8\u4ecb\u7ecd&lt;\/a&gt;\r\n    &lt;!-- \u8def\u7531\u8f93\u51fa \u76f8\u5f53\u4e8erouter-view --&gt;\r\n    &lt;div id=\"router-view\"&gt;&lt;\/div&gt;\r\n\r\n    &lt;script src=\".\/js\/router.js\"&gt;&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n<\/code><\/pre>\n<p>&nbsp;<\/li>\n<\/ul>\n<ul>\n<li>\u521b\u5efa router.js \u6587\u4ef6, \u65b9\u4fbf\u7406\u89e3\u5c06 \u7ed3\u6784\u5199\u6765\u4e0evue \u6846\u67b6router.js\u7c7b\u4f3c\n<pre><code class=\"language-js\" lang=\"js\">\/**\r\n * \u521b\u5efa\u7ec4\u4ef6\r\n *   \u4ec5\u4e3a\u4e00\u4e2adiv\r\n * @param {*} id\r\n * @param {*} text\r\n * @returns\r\n *\/\r\nconst importCom = (id, text) =&gt; {\r\n\tconst el = document.createElement('div')\r\n\tel.id = id\r\n\tel.innerHTML = text\r\n\treturn el\r\n}\r\n\r\n\/**\r\n * \u8def\u7531\u8868\r\n *\/\r\nconst routes = [\r\n\t{\r\n\t\tpath: 'home',\r\n\t\tcomponent: importCom('home', '\u6b22\u8fce\u5149\u4e34\u516c\u53f8\u9996\u9875'),\r\n\t},\r\n\t{\r\n\t\tpath: 'about',\r\n\t\tcomponent: importCom('about', '\u4ee5\u5ba2\u6237\u4e3a\u4e2d\u5fc3\uff0c\u4ee5\u594b\u6597\u8005\u4e3a\u672c!'),\r\n\t},\r\n\t{\r\n\t\tpath: 'introduce',\r\n\t\tcomponent: importCom('introduce', '\u4e92\u8054\u7f51\u77e5\u540d\u4f01\u4e1a'),\r\n\t},\r\n\t{\r\n\t\tpath: 'notFound',\r\n\t\tcomponent: importCom('notFound', '\u9875\u9762\u4e0d\u80fd\u627e\u5230!'),\r\n\t},\r\n]\r\n\/**\r\n * \u6dfb\u52a0\u8def\u7531\uff0c\u9ed8\u8ba4home\r\n *\/\r\nconst router = () =&gt; {\r\n\tconst url = location.hash.slice(1) || 'home' \/\/ \u83b7\u53d6hash\u503c\r\n\tconst rootEle = document.querySelector('#router-view') \/\/ \u8def\u7531\u8f93\u51fa\u6839\u8282\u70b9\r\n\trootEle.innerHTML = '' \/\/\u6e05\u7a7a\u539f\u5185\u5bb9\r\n\tconst childEle = routes.find(item =&gt; item.path == url)?.component \/\/ \u67e5\u627e\u8def\u7531\u7ec4\u4ef6\r\n\t\/\/ \u6dfb\u52a0\u8def\u7531\u7ec4\u4ef6\r\n\trootEle.appendChild(childEle ? childEle : routes.find(item =&gt; item.path == 'notFound')?.component)\r\n}\r\n\r\nrouter() \/\/\u521d\u59cb\u5316\r\n\r\n\/\/ hash\u8def\u7531\u5207\u6362\u76d1\u542c,hash\u503c\u53d8\u5316\u89e6\u53d1\r\nwindow.addEventListener('hashchange', router)\r\n\r\n<\/code><\/pre>\n<p>&nbsp;<\/li>\n<\/ul>\n<ul>\n<li>\u5230\u8fd9\u91cc\u5c31\u5df2\u7ecf\u5b9e\u73b0\u4e86\u4e00\u4e2ahash\u8def\u7531\u5207\u6362demo\uff0c\u4f60\u53ef\u4ee5\u6253\u5230\u6d4f\u89c8\u5668\u8fd0\u884cindex.html \u6d4b\u8bd5<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/08\/hash.gif\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3353\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/08\/hash.gif\" alt=\"hash\" width=\"634\" height=\"279\" \/><\/a><\/li>\n<\/ul>\n<ol>\n<li>history \u5982\u4f55\u5b9e\u73b0\u5462\uff1f\u6211\u4eec\u76f4\u63a5\u770b\u4ee3\u7801\n<ul>\n<li>\u65b0\u5efa index.html\u9875\u9762\n<pre><code class=\"language-html\" lang=\"html\">&lt;body&gt;\r\n    &lt;!-- \u76f8\u5f53\u4e8e router-link --&gt;\r\n    &lt;nav id='nav'&gt;\r\n       &lt;a href=\".\/home\"&gt;\u9996\u9875&lt;\/a&gt; | &lt;a href=\".\/about\"&gt;\u5173\u4e8e\u6211\u4eec&lt;\/a&gt; | &lt;a href=\".\/introduce\"&gt;\u516c\u53f8\u4ecb\u7ecd&lt;\/a&gt;\r\n    &lt;\/nav&gt;\r\n    &lt;!-- \u8def\u7531\u8f93\u51fa \u76f8\u5f53\u4e8erouter-view --&gt;\r\n    &lt;div id=\"router-view\"&gt;&lt;\/div&gt;\r\n\r\n    &lt;script src=\".\/js\/router.js\"&gt;&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n<\/code><\/pre>\n<\/li>\n<li>\u521b\u5efa router.js \u6587\u4ef6, \u4e0ehash router.js\u5927\u90e8\u5206\u76f8\u540c\n<pre><code class=\"language-js \" lang=\"js \">\/**\r\n * \u521b\u5efa\u7ec4\u4ef6\r\n *   \u4ec5\u4e3a\u4e00\u4e2adiv\r\n * @param {*} id\r\n * @param {*} text\r\n * @returns\r\n *\/\r\nconst importCom = (id, text) =&gt; {\r\n\tconst el = document.createElement('div')\r\n\tel.id = id\r\n\tel.innerHTML = text\r\n\treturn el\r\n}\r\n\r\n\/**\r\n * \u8def\u7531\u8868\r\n *\/\r\nconst routes = [\r\n\t{\r\n\t\tpath: 'home',\r\n\t\tcomponent: importCom('home', '\u6b22\u8fce\u5149\u4e34\u516c\u53f8\u9996\u9875'),\r\n\t},\r\n\r\n\t{\r\n\t\tpath: 'about',\r\n\t\tcomponent: importCom('about', '\u4ee5\u5ba2\u6237\u4e3a\u4e2d\u5fc3\uff0c\u4ee5\u594b\u6597\u8005\u4e3a\u672c!'),\r\n\t},\r\n\t{\r\n\t\tpath: 'introduce',\r\n\t\tcomponent: importCom('introduce', '\u4e92\u8054\u7f51\u77e5\u540d\u4f01\u4e1a'),\r\n\t},\r\n\t{\r\n\t\tpath: 'notFound',\r\n\t\tcomponent: importCom('notFound', '\u9875\u9762\u4e0d\u80fd\u627e\u5230!'),\r\n\t},\r\n]\r\n\/**\r\n * \u6dfb\u52a0\u8def\u7531\uff0c\u9ed8\u8ba4home\r\n *\/\r\nconst router = () =&gt; {\r\n\tconst url = location.pathname.slice(1) || 'home'\r\n\tconst rootEle = document.querySelector('#router-view')\r\n\trootEle.innerHTML = ''\r\n\tconst childEle = routes.find(item =&gt; item.path == url)?.component\r\n\trootEle.appendChild(childEle ? childEle : routes.find(item =&gt; item.path == 'notFound')?.component)\r\n}\r\n\r\n\/\/\u4e8b\u4ef6\u59d4\u6258\u7ed9a\u6807\u7b7e\u7ed1\u5b9a\u4e8b\u4ef6\uff0c\u76d1\u542c\u5207\u6362\r\nconst navEle = document.querySelector('#nav')\r\nnavEle.addEventListener('click', function (e) {\r\n\tif (e.target.tagName == 'A') {\r\n\t\te.preventDefault()\r\n\t\tconst href = e.target.getAttribute('href').slice(1)\r\n\r\n\t\thistory.pushState(null, `\u9875\u9762\u6807\u9898`, href)  \/\/ \u76f8\u5f53\u4e8e push\r\n\t\t\/\/ history.replaceState(null, `\u9875\u9762\u6807\u9898`, href)  \/\/\u76f8\u5f53\u4e8ereplace\r\n    \r\n\t\trouter()  \/\/ \u8fd9\u91cc\u4e0d\u8981\u5fd8\u8bb0\u6267\u884c\r\n\t}\r\n})\r\n\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u8def\u7531\u6a21\u5f0f\u9664\u4e86\u4ee5\u4e0a\u4e24\u79cd\u8fd8\u6709memory\u6a21\u5f0f , \u8fd9\u4e2a\u5728react\u6846\u67b6\u662f\u4e0d\u5426\u89c1\u8fc7\u5462\uff011\u3001\u4f18\u70b9\u9002\u5408\u975e\u6d4f\u89c8\u5668\u4e0d\u9700\u8981url\u8def\u5f84\uff0c\u53ef\u4ee5\u4e0d\u4f9d\u8d56\u7f51\u9875\n<p>\u65e0\u9700\u5237\u65b0\u9875\u9762<\/p>\n<p>2\u3001\u7f3a\u70b9<\/p>\n<p>\u9002\u7528\u573a\u666f\u5c11\uff0c\u901a\u5e38\u5728app\u4e2d\u4f7f\u7528<\/p>\n<p>\u53ea\u5bf9\u5355\u673a\u6709\u6548<\/p>\n<p>\u65e0\u6cd5\u5b9e\u73b0\u5206\u4eab\u7b49\u9700\u8981\u4f7f\u7528url\u7684\u529f\u80fd<\/p>\n<p>\u4e0d\u652f\u6301\u6d4f\u89c8\u5668\u524d\u8fdb\u540e\u9000<\/p>\n<p>3\u3001\u539f\u7406<\/p>\n<p>\u4f7f\u7528\u672c\u5730\u5b58\u50a8localstorage\uff0c\u5bf9\u8def\u5f84\u8fdb\u884c\u5b58\u50a8 , \u4e0a\u9762 history\u6a21\u5f0f\u5b9e\u73b0\uff0c\u5b58\u50a8\u8def\u5f84\u6362\u6210localstorage\u5c31\u5b8c\u4e8b\u3002<\/li>\n<li>\u76f8\u5173\u9605\u8bfb: <a title=\"\u8be6\u7ec6\u9605\u8bfb \u4e24\u79cd\u524d\u7aef\u8def\u7531\uff08hash\u6a21\u5f0f\u548chistory\u6a21\u5f0f\uff09\u5b9e\u73b0\u539f\u7406\u89e3\u6790\u53ca\u533a\u522b\" href=\"https:\/\/ai.zhousir.top\/archives\/2514\">\u4e24\u79cd\u524d\u7aef\u8def\u7531\uff08hash\u6a21\u5f0f\u548chistory\u6a21\u5f0f\uff09\u5b9e\u73b0\u539f\u7406\u89e3\u6790\u53ca\u533a\u522b<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u8bd5vue\u5f00\u53d1\u65f6\uff0c\u9762\u8bd5\u5b98\u7ecf\u5e38\u95ee: \u8bf7\u8bf4\u4e0b vue\u6846\u67b6\u8def\u7531\u6a21\u5f0f\u6709\u90a3\u4e24\u79cd.\u4f60\u80af\u5b9a\u4f1a\u6beb\u4e0d\u72b9\u8c6b\u7b54\u51fa: hash\u8def\u7531\u548ch [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[106,105],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3350"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3350"}],"version-history":[{"count":11,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3350\/revisions"}],"predecessor-version":[{"id":3364,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3350\/revisions\/3364"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}