{"id":1000,"date":"2023-03-12T08:21:05","date_gmt":"2023-03-12T00:21:05","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1000"},"modified":"2023-04-29T13:19:13","modified_gmt":"2023-04-29T05:19:13","slug":"vue-js-route-fallback-to-specified-page","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/vue-js-route-fallback-to-specified-page\/","title":{"rendered":"Vue.js\u8def\u7531\u56de\u9000\u5230\u6307\u5b9a\u9875\u9762"},"content":{"rendered":"<h2>vue-router<\/h2>\n<pre><code>\/\/\u4f1a\u8bb0\u5f55\u8df3\u8f6c\u7684\u8def\u7531\uff0c\u5e76\u5b58\u5728 history \u6808\u91cc\uff08\u5177\u6709length\u5c5e\u6027\uff09\uff0c\u9002\u5408\u4e0d\u540c\u9875\u9762\u7684\u5207\u6362\nrouter.push();\n\n\/\/\u4e0d\u4f1a\u8bb0\u5f55\u8def\u7531\uff0c\u56de\u9000\u4e0d\u5230\u4e0a\u4e00\u4e2a\u9875\u9762\uff0c\u9002\u5408\u5355\u9875\u9762\u7684\u5207\u6362\nrouter.replace();\n\n\/\/\u53ef\u5bf9\u6bd4history.go()\u7684\u7528\u6cd5\nrouter.go();<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>router\u5bfc\u822a\u5b88\u536b<\/h2>\n<p>router\u5bfc\u822a\u5b88\u536b\u6709\u5168\u5c40\u7684\u3001\u8def\u7531\u72ec\u4eab\u7684\u3001\u7ec4\u4ef6\u7684\u3002<\/p>\n<p>\u5047\u8bbe\u4eceA\u9875\u9762 -&gt; B\u9875\u9762 -&gt; C \u9875\u9762\uff0c\u5982\u679c\u6309\u56de\u9000\u6309\u94ae\u7684\u8bdd\uff0c\u60f3\u56de\u5230A\u9875\u9762\uff0c\u800c\u4e0d\u662f\u6b63\u5e38\u7684\u56de\u9000\u5230B\u9875\u9762\uff0c\u8be5\u600e\u4e48\u5b9e\u73b0\u5462\uff1f<\/p>\n<pre><code class=\"language-javascript\">\/\/router --&gt; index.js\n\nimport Vue from &#039;vue&#039;\nimport Router from &#039;vue-router&#039;\nimport a from &#039;@\/components\/a&#039; \/\/a \u7ec4\u4ef6\nimport b from &#039;@\/components\/b&#039; \/\/b \u7ec4\u4ef6\nimport c from &#039;@\/components\/c&#039; \/\/c \u7ec4\u4ef6 \n\nVue.use(Router)\n\nexport default new Router({\n  routes: [\n    {\n      path: &#039;\/&#039;,\n      component: a\n    },\n    {\n      path: &#039;\/a&#039;,\n      component: a\n    },\n    {\n      path: &#039;\/b&#039;,\n      component: b,\n      beforeEnter: (to, from, next) =&gt; {\n        from.path == &#039;\/c&#039; ? next(&#039;\/&#039;) :next();\n      }\n      \/\/beforeEnter \u5728\u8fdb\u5165\u8fd9\u4e2a\u8def\u7531\u4e4b\u524d\uff0c\u5148\u5224\u65ad\u662f\u4ece\u54ea\u4e2a\u8def\u7531\u8df3\u8f6c\u7684\n    },\n    {\n      path: &#039;\/c&#039;,\n      component: c\n    }\n  ]\n})<\/code><\/pre>\n<blockquote>\n<p>\u5728\u8def\u7531\u72ec\u4eab\u7684\u5b88\u536b\uff0c\u4f7f\u7528next(&#8216;\/&#8217;)\uff0c\u6216\u8005\u5176\u4ed6\u8def\u7531\uff0c\u662f\u4e0d\u4f1a\u62a5\u9519\u7684\u3002\u4f46\u662f\u5982\u679c\u5728\u7ec4\u4ef6\u91cc\u4f7f\u7528\uff0c\u662f\u4f1a\u62a5\u9519\u7684\uff0c\u5728\u7ec4\u4ef6\u91cc\u53ea\u80fd\u4f7f\u7528\uff0c\u4e5f\u5fc5\u987b\u8981\u8c03\u7528next()<\/p>\n<\/blockquote>\n<h2>history.length<\/h2>\n<p>\u8fd0\u884c\u9879\u76ee\uff0c\u6253\u5f00\u63a7\u5236\u53f0\uff0c\u5206\u522b\u6253\u5370\u4e0b<code>history<\/code>\u548c<code>history.length<\/code>\uff0c\u53ef\u4ee5\u770b\u5230\u8def\u7531\u662f\u4fdd\u7559\u5728\u6570\u7ec4\u91cc\u7684\uff0c\u5e76\u4e14\u80fd\u83b7\u5f97length\u503c\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u9996\u9875<\/p>\n<pre><code class=\"language-javascript\">let len = history.length;\nhistory.go(-(len-1));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>vue-router \/\/\u4f1a\u8bb0\u5f55\u8df3\u8f6c\u7684\u8def\u7531\uff0c\u5e76\u5b58\u5728 history \u6808\u91cc\uff08\u5177\u6709length\u5c5e\u6027\uff09\uff0c\u9002\u5408\u4e0d\u540c\u9875\u9762 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1000","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1000"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1000\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}