{"id":295494,"date":"2024-05-20T15:37:24","date_gmt":"2024-05-20T07:37:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/295494.html"},"modified":"2024-05-20T15:37:36","modified_gmt":"2024-05-20T07:37:36","slug":"vue3-%e4%b8%ad%e5%a6%82%e4%bd%95%e7%a6%81%e6%ad%a2%e6%b5%8f%e8%a7%88%e5%99%a8%e5%90%8e%e9%80%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/295494.html","title":{"rendered":"vue3 \u4e2d\u5982\u4f55\u7981\u6b62\u6d4f\u89c8\u5668\u540e\u9000"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27142808\/39935e36-58e3-49ac-8f32-fa64410481de.webp\" alt=\"vue3 \u4e2d\u5982\u4f55\u7981\u6b62\u6d4f\u89c8\u5668\u540e\u9000\" \/><\/p>\n<p><p>\u5728Vue3\u4e2d\u7981\u6b62\u6d4f\u89c8\u5668\u540e\u9000\u53ef\u4ee5\u901a\u8fc7<strong>\u7f16\u7a0b\u5f0f\u5bfc\u822a\u7684\u524d\u7f6e\u5b88\u536b<\/strong>\u3001<strong>HTML5 History API<\/strong>\u4ee5\u53ca<strong>window\u5bf9\u8c61\u7684popstate\u4e8b\u4ef6<\/strong>\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u963b\u6b62\u7528\u6237\u89e6\u53d1\u6d4f\u89c8\u5668\u7684\u540e\u9000\u64cd\u4f5c\uff0c\u4fdd\u62a4\u5e94\u7528\u7a0b\u5e8f\u7684\u9875\u9762\u72b6\u6001\u4e0d\u88ab\u610f\u5916\u6539\u53d8\u3002\u5176\u4e2d\uff0c<strong>\u7f16\u7a0b\u5f0f\u5bfc\u822a\u7684\u524d\u7f6e\u5b88\u536b<\/strong>\u53ef\u4ee5\u5728Vue\u8def\u7531\u5668\u4e2d\u8bbe\u7f6e\uff0c\u5229\u7528\u8def\u7531\u5b88\u536bbeforeEach\u51fd\u6570\u6765\u963b\u6b62\u5bfc\u822a\u53d1\u751f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Vue\u5e94\u7528\u7684\u8def\u7531\u8bbe\u7f6e\u4e2d\u52a0\u5165\u524d\u7f6e\u5b88\u536b\uff0c\u901a\u8fc7\u8c03\u7528<code>next(false)<\/code>\u6765\u963b\u6b62\u6d4f\u89c8\u5668\u7684\u540e\u9000\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const router = createRouter({<\/p>\n<p>  \/\/ ...\u5176\u4ed6\u8def\u7531\u914d\u7f6e<\/p>\n<p>});<\/p>\n<p>router.beforeEach((to, from, next) =&gt; {<\/p>\n<p>  if (shouldPreventBack(to, from)) {<\/p>\n<p>    \/\/ \u963b\u6b62\u540e\u9000\u64cd\u4f5c<\/p>\n<p>    next(false);<\/p>\n<p>  } else {<\/p>\n<p>    next();<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>shouldPreventBack<\/code>\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u5728\u4ecefrom\u8def\u7531\u5230to\u8def\u7531\u7684\u8f6c\u6362\u4e2d\u662f\u5426\u9700\u8981\u963b\u6b62\u540e\u9000\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u914d\u7f6e\u8def\u7531\u5b88\u536b<\/strong><\/h3>\n<\/p>\n<p><p>\u8bbe\u7f6e\u8def\u7531\u5b88\u536b\u662fVue3\u4e2d\u9632\u6b62\u9875\u9762\u540e\u9000\u7684\u5e38\u7528\u65b9\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528<code>router.beforeEach()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u8def\u7531\u53d1\u751f\u53d8\u5316\u4e4b\u524d\u8fdb\u884c\u68c0\u67e5\uff0c\u5e76\u51b3\u5b9a\u662f\u5426\u8ba9\u8def\u7531\u53d8\u5316\u7ee7\u7eed\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">import { createRouter, createWebHistory } from &#039;vue-router&#039;;<\/p>\n<p>const router = createRouter({<\/p>\n<p>  history: createWebHistory(),<\/p>\n<p>  routes: [<\/p>\n<p>    \/\/ \u8def\u7531\u914d\u7f6e...<\/p>\n<p>  ],<\/p>\n<p>});<\/p>\n<p>router.beforeEach((to, from, next) =&gt; {<\/p>\n<p>  if (from.path === &#039;\/protected&#039; &amp;&amp; shouldPreventBack()) {<\/p>\n<p>    next(false);<\/p>\n<p>  } else {<\/p>\n<p>    next();<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p>function shouldPreventBack() {<\/p>\n<p>  \/\/ \u8fd9\u91cc\u53ef\u4ee5\u6dfb\u52a0\u903b\u8f91\u5224\u65ad\uff0c\u51b3\u5b9a\u662f\u5426\u9700\u8981\u963b\u6b62\u540e\u9000<\/p>\n<p>  return true; \/\/ \u793a\u4f8b\u4e2d\u9ed8\u8ba4\u603b\u662f\u963b\u6b62\u540e\u9000<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u4f7f\u7528History API<\/strong><\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u76f4\u63a5\u64cd\u4f5cHTML5 History API\u3002\u5f53\u7528\u6237\u6267\u884c\u540e\u9000\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728window\u5bf9\u8c61\u4e0a\u76d1\u542c<code>popstate<\/code>\u4e8b\u4ef6\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function disableBack() {<\/p>\n<p>  window.history.pushState(null, null, window.location.href);<\/p>\n<p>  window.addEventListener(&#039;popstate&#039;, function (event) {<\/p>\n<p>    window.history.pushState(null, null, window.location.href);<\/p>\n<p>  });<\/p>\n<p>}<\/p>\n<p>\/\/ \u5728\u7ec4\u4ef6\u6216\u8005\u5e94\u7528\u521d\u59cb\u5316\u65f6\u8c03\u7528<\/p>\n<p>disableBack();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u7ed3\u5408Vue\u548cHistory API<\/strong><\/h3>\n<\/p>\n<p><p>\u7ed3\u5408Vue\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u548cHTML5 History API\uff0c\u53ef\u4ee5\u66f4\u52a0\u7cbe\u7ec6\u5730\u63a7\u5236\u9875\u9762\u5982\u4f55\u54cd\u5e94\u540e\u9000\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">import { onMounted, onUnmounted } from &#039;vue&#039;;<\/p>\n<p>export default {<\/p>\n<p>  setup() {<\/p>\n<p>    onMounted(() =&gt; {<\/p>\n<p>      window.history.pushState(null, null, window.location.href);<\/p>\n<p>      window.addEventListener(&#039;popstate&#039;, handlePopstate);<\/p>\n<p>    });<\/p>\n<p>    onUnmounted(() =&gt; {<\/p>\n<p>      window.removeEventListener(&#039;popstate&#039;, handlePopstate);<\/p>\n<p>    });<\/p>\n<p>    function handlePopstate(event) {<\/p>\n<p>      window.history.pushState(null, null, window.location.href);<\/p>\n<p>    }<\/p>\n<p>  },<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u56db\u3001\u63d0\u9192\u7528\u6237\u65e0\u6cd5\u540e\u9000<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u9700\u8981\u4e25\u683c\u5730\u7981\u6b62\u540e\u9000\u64cd\u4f5c\uff0c\u800c\u662f\u63d0\u9192\u7528\u6237\u4ed6\u4eec\u5f53\u524d\u4e0d\u80fd\u540e\u9000\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">router.beforeEach((to, from, next) =&gt; {<\/p>\n<p>  if (from.path === &#039;\/protected&#039; &amp;&amp; shouldPreventBack()) {<\/p>\n<p>    alert(&quot;\u5f53\u524d\u9875\u9762\u7981\u6b62\u540e\u9000&quot;);<\/p>\n<p>    next(false);<\/p>\n<p>  } else {<\/p>\n<p>    next();<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u4f53\u800c\u8a00\uff0c\u7981\u6b62\u6d4f\u89c8\u5668\u7684\u540e\u9000\u64cd\u4f5c\u9700\u8981\u8c28\u614e\u4f7f\u7528\uff0c\u786e\u4fdd\u4e0d\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\uff0c\u5e76\u786e\u4fdd\u903b\u8f91\u4e0a\u7684\u5408\u7406\u548c\u5fc5\u8981\u6027\u3002\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406Vue3\u5e94\u7528\u4e2d\u7684\u5bfc\u822a\u884c\u4e3a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u963b\u6b62\u6d4f\u89c8\u5668\u56de\u9000\u529f\u80fd\uff1f<\/strong><\/p>\n<p>\u5728Vue3\u4e2d\uff0c\u963b\u6b62\u6d4f\u89c8\u5668\u56de\u9000\u529f\u80fd\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u5728\u9700\u8981\u963b\u6b62\u56de\u9000\u7684\u9875\u9762\u7684\u94a9\u5b50\u51fd\u6570\u4e2d\uff0c\u4f8b\u5982created\u6216mounted\uff0c\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>window.history.pushState(null, document.title, window.location.href);\nwindow.addEventListener(&#039;popstate&#039;, function (event) {\n    window.history.pushState(null, document.title, window.location.href);\n});\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5f53\u7528\u6237\u70b9\u51fb\u6d4f\u89c8\u5668\u56de\u9000\u6309\u94ae\u65f6\uff0c\u9875\u9762\u4e0d\u4f1a\u540e\u9000\uff0c\u800c\u662f\u4fdd\u6301\u5728\u5f53\u524d\u9875\u9762\u3002<\/p>\n<p><strong>2. \u6211\u60f3\u5728Vue3\u4e2d\u7981\u7528\u6d4f\u89c8\u5668\u7684\u540e\u9000\u529f\u80fd\uff0c\u8be5\u600e\u4e48\u529e\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u5728Vue3\u4e2d\u7981\u7528\u6d4f\u89c8\u5668\u7684\u540e\u9000\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u5728\u9700\u8981\u7981\u7528\u540e\u9000\u7684\u9875\u9762\u7684\u94a9\u5b50\u51fd\u6570\u4e2d\uff0c\u4f8b\u5982created\u6216mounted\uff0c\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>window.addEventListener(&#039;popstate&#039;, function (event) {\n    window.history.forward(1);\n});\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5f53\u7528\u6237\u70b9\u51fb\u6d4f\u89c8\u5668\u56de\u9000\u6309\u94ae\u65f6\uff0c\u9875\u9762\u4f1a\u81ea\u52a8\u524d\u8fdb\u5230\u4e0b\u4e00\u4e2a\u9875\u9762\uff0c\u7981\u7528\u4e86\u540e\u9000\u529f\u80fd\u3002<\/p>\n<p><strong>3. \u600e\u6837\u5728Vue3\u4e2d\u9650\u5236\u6d4f\u89c8\u5668\u7684\u540e\u9000\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u5728Vue3\u4e2d\u9650\u5236\u6d4f\u89c8\u5668\u7684\u540e\u9000\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u8bbe\u7f6e\u3002\u9996\u5148\uff0c\u5728\u9700\u8981\u9650\u5236\u540e\u9000\u7684\u9875\u9762\u7684\u94a9\u5b50\u51fd\u6570\u4e2d\uff0c\u4f8b\u5982created\u6216mounted\uff0c\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>window.addEventListener(&#039;popstate&#039;, function (event) {\n    event.preventDefault();\n    window.history.forward(1);\n});\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5f53\u7528\u6237\u70b9\u51fb\u6d4f\u89c8\u5668\u56de\u9000\u6309\u94ae\u65f6\uff0c\u9875\u9762\u65e2\u4e0d\u4f1a\u540e\u9000\uff0c\u4e5f\u65e0\u6cd5\u524d\u8fdb\u5230\u4e0b\u4e00\u4e2a\u9875\u9762\uff0c\u5b9e\u73b0\u4e86\u5bf9\u6d4f\u89c8\u5668\u540e\u9000\u64cd\u4f5c\u7684\u9650\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Vue3\u4e2d\u7981\u6b62\u6d4f\u89c8\u5668\u540e\u9000\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u5f0f\u5bfc\u822a\u7684\u524d\u7f6e\u5b88\u536b\u3001HTML5 History API\u4ee5\u53cawindow\u5bf9\u8c61 [&hellip;]","protected":false},"author":3,"featured_media":295518,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295494"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=295494"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295494\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/295518"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=295494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=295494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=295494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}