{"id":91325,"date":"2024-04-09T18:26:00","date_gmt":"2024-04-09T10:26:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/91325.html"},"modified":"2024-04-09T18:26:01","modified_gmt":"2024-04-09T10:26:01","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8javascript%e4%b8%ad%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%ae%9a%e4%b9%89%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/91325.html","title":{"rendered":"\u5982\u4f55\u5728JavaScript\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8def\u7531"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02151142\/7fdea190-b328-44ea-8248-a1bafbea1232.webp\" alt=\"\u5982\u4f55\u5728JavaScript\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8def\u7531\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8def\u7531\u4e3b\u8981\u6d89\u53ca\u5230\u4e86\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<strong>\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u3001\u76d1\u542cURL\u53d8\u5316\u3001\u5339\u914d\u8def\u7531\u89c4\u5219\u3001\u6e32\u67d3\u5bf9\u5e94\u7ec4\u4ef6<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u76d1\u542cURL\u53d8\u5316<\/strong>\u662f\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8def\u7531\u7684\u6838\u5fc3\u3002\u73b0\u4ee3\u6d4f\u89c8\u5668\u63d0\u4f9b\u4e86<code>history<\/code> API\uff0c\u901a\u8fc7\u8fd9\u4e2aAPI\u53ef\u4ee5\u76d1\u542c\u548c\u64cd\u4f5c\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u3002\u901a\u8fc7\u76d1\u542c<code>popstate<\/code>\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5728URL\u6539\u53d8\u65f6\u89e6\u53d1\u76f8\u5e94\u7684\u8def\u7531\u903b\u8f91\uff0c\u800c\u65e0\u9700\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u3002\u8fd9\u6837\u5c31\u80fd\u5b9e\u73b0\u4e00\u4e2a\u5355\u9875\u5e94\u7528\uff08SPA\uff09\u7684\u8def\u7531\u7cfb\u7edf\u3002\u901a\u8fc7<code>history.pushState<\/code>\u548c<code>history.replaceState<\/code>\u65b9\u6cd5\uff0c\u80fd\u591f\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u6539\u53d8\u6d4f\u89c8\u5668\u7684URL\uff0c\u914d\u5408\u76d1\u542c<code>popstate<\/code>\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u524d\u7aef\u8def\u7531\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u8def\u7531\u89c4\u5219<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u662f\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8def\u7531\u7684\u7b2c\u4e00\u6b65\u3002\u901a\u5e38\uff0c\u8def\u7531\u89c4\u5219\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u952e\u901a\u5e38\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u8868\u793a\u8def\u7531\u7684\u8def\u5f84\uff1b\u503c\u5219\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u662f\u5bf9\u8c61\uff0c\u7528\u4e8e\u63cf\u8ff0\u5f53\u8def\u7531\u5339\u914d\u65f6\u5e94\u8be5\u6267\u884c\u7684\u64cd\u4f5c\u6216\u8005\u6e32\u67d3\u7684\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8def\u7531\u8868\u8bbe\u8ba1<\/strong>\uff1a\u4f60\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684JavaScript\u5bf9\u8c61\u6765\u5b58\u50a8\u8def\u7531\u8def\u5f84\u4e0e\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>routes = {&#039;\/home&#039;: homeComponent, &#039;\/about&#039;: aboutComponent}<\/code>\uff0c\u8fd9\u91cc\u7684<code>homeComponent<\/code>\u548c<code>aboutComponent<\/code>\u53ef\u4ee5\u662f\u7528\u4e8e\u6e32\u67d3\u5bf9\u5e94\u9875\u9762\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8def\u7531<\/strong>\uff1a\u4e3a\u4e86\u8ba9\u8def\u7531\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u8def\u7531\u8def\u5f84\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u52a8\u6001\u8def\u7531\uff0c\u4f8b\u5982\uff1a<code>&#039;\/user\/:id&#039;<\/code>\uff0c\u5176\u4e2d<code>:id<\/code>\u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u7528\u6237ID\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u76d1\u542cURL\u53d8\u5316<\/h3>\n<\/p>\n<p><p>\u76d1\u542cURL\u53d8\u5316\u662f\u81ea\u5b9a\u4e49\u8def\u7531\u5b9e\u73b0\u4e2d\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u5b83\u5141\u8bb8\u4f60\u5728URL\u53d8\u5316\u65f6\u6267\u884c\u76f8\u5e94\u7684\u8def\u7531\u903b\u8f91\uff0c\u800c\u4e0d\u9700\u8981\u9875\u9762\u91cd\u65b0\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5229\u7528<code>popstate<\/code>\u4e8b\u4ef6<\/strong>\uff1a\u6d4f\u89c8\u5668\u7684<code>history<\/code> API\u5141\u8bb8\u6211\u4eec\u76d1\u542c<code>popstate<\/code>\u4e8b\u4ef6\uff0c\u8fd9\u4e2a\u4e8b\u4ef6\u5728\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\uff08\u6bd4\u5982\u7528\u6237\u70b9\u51fb\u6d4f\u89c8\u5668\u7684\u540e\u9000\u6309\u94ae\uff09\u3002\u901a\u8fc7\u76d1\u542c\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5728URL\u53d8\u5316\u65f6\u89e6\u53d1\u76f8\u5e94\u7684\u8def\u7531\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u64cd\u4f5c\u5386\u53f2\u8bb0\u5f55<\/strong>\uff1a\u4f7f\u7528<code>history.pushState<\/code>\u548c<code>history.replaceState<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u6d4f\u89c8\u5668\u7684URL\u3002\u8fd9\u5bf9\u4e8e\u521b\u5efa\u4e00\u4e2a\u65e0\u5237\u65b0\u7f51\u9875\u5e94\u7528\uff08SPA\uff09\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u4f60\u9700\u8981\u5728\u8def\u7531\u903b\u8f91\u4e2d\u5408\u9002\u7684\u65f6\u673a\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u53cd\u6620\u5e94\u7528\u7684\u5f53\u524d\u72b6\u6001\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5339\u914d\u8def\u7531\u89c4\u5219<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u6211\u4eec\u76d1\u542c\u5230URL\u7684\u53d8\u5316\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u6839\u636e\u5f53\u524d\u7684URL\u53bb\u5339\u914d\u6211\u4eec\u9884\u5b9a\u4e49\u7684\u8def\u7531\u89c4\u5219\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u89e3\u6790\u5f53\u524dURL<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u89e3\u6790\u5f53\u524d\u7684URL\u8def\u5f84\uff0c\u4ee5\u786e\u5b9a\u7528\u6237\u8bd5\u56fe\u8bbf\u95ee\u7684\u662f\u54ea\u4e2a\u8def\u7531\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u89e3\u6790URL\u7684\u8def\u5f84\u90e8\u5206\uff0c\u5e76\u5c06\u5176\u548c\u8def\u7531\u8868\u4e2d\u5b9a\u4e49\u7684\u8def\u5f84\u8fdb\u884c\u5339\u914d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u8def\u7531\u5339\u914d<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u8def\u7531\u89c4\u5219\u4e2d\u5305\u542b\u4e86\u52a8\u6001\u90e8\u5206\uff08\u4f8b\u5982<code>:id<\/code>\uff09\uff0c\u8fd9\u65f6\u5019\u4f60\u9700\u8981\u66f4\u590d\u6742\u7684\u5339\u914d\u903b\u8f91\u6765\u63d0\u53d6\u8fd9\u4e9b\u52a8\u6001\u53c2\u6570\u3002\u8fd9\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5c06\u8def\u7531\u8def\u5f84\u5b9a\u4e49\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u5728\u5339\u914d\u65f6\u4eceURL\u4e2d\u63d0\u53d6\u76f8\u5e94\u7684\u53c2\u6570\u503c\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u6e32\u67d3\u5bf9\u5e94\u7ec4\u4ef6<\/h3>\n<\/p>\n<p><p>\u5339\u914d\u5230\u5177\u4f53\u7684\u8def\u7531\u89c4\u5219\u540e\uff0c\u6700\u540e\u4e00\u6b65\u662f\u6839\u636e\u8def\u7531\u7684\u6307\u5411\u6765\u6e32\u67d3\u76f8\u5e94\u7684\u9875\u9762\u6216\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8c03\u7528\u8def\u7531\u5904\u7406\u51fd\u6570<\/strong>\uff1a\u6bcf\u4e2a\u8def\u7531\u89c4\u5219\u90fd\u5e94\u8be5\u5173\u8054\u4e00\u4e2a\u5904\u7406\u51fd\u6570\u6216\u8005\u662f\u7ec4\u4ef6\u3002\u5f53URL\u4e0e\u67d0\u4e2a\u8def\u7531\u5339\u914d\u65f6\uff0c\u5c31\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u8d1f\u8d23\u6e32\u67d3\u9875\u9762\u6216\u6267\u884c\u5176\u4ed6\u5fc5\u8981\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ec4\u4ef6\u6e32\u67d3<\/strong>\uff1a\u5728SPA\u4e2d\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e2a\u201c\u5bb9\u5668\u201d\u5143\u7d20\uff0c\u7528\u4e8e\u52a8\u6001\u5730\u52a0\u8f7d\u548c\u663e\u793a\u4e0d\u540c\u7684\u9875\u9762\u7ec4\u4ef6\u3002\u5728\u8def\u7531\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u521b\u5efa\u65b0\u7684\u7ec4\u4ef6\u5b9e\u4f8b\uff0c\u5e76\u5c06\u5176\u6e32\u67d3\u5230\u5bb9\u5668\u5143\u7d20\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728JavaScript\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u81ea\u5b9a\u4e49\u8def\u7531\u673a\u5236\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4e3a\u4f60\u7684\u8def\u7531\u5668\u6dfb\u52a0\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u8def\u7531\u5b88\u536b\u3001\u61d2\u52a0\u8f7d\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Question 1: \u5728JavaScript\u4e2d\u5982\u4f55\u7f16\u5199\u81ea\u5b9a\u4e49\u8def\u7531\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8981\u7f16\u5199\u81ea\u5b9a\u4e49\u8def\u7531\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684\u4e00\u4e9b\u5e93\u6216\u6846\u67b6\uff0c\u6bd4\u5982React Router\u6216Vue Router\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684API\u548c\u7ec4\u4ef6\uff0c\u4ee5\u4fbf\u4f60\u80fd\u591f\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u8def\u7531\u529f\u80fd\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4ece\u5b89\u88c5\u76f8\u5173\u7684\u5e93\u5f00\u59cb\uff0c\u7136\u540e\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\u6587\u4ef6\u4e2d\u914d\u7f6e\u8def\u7531\u3002\u901a\u5e38\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u8def\u7531\u7684\u8def\u5f84\u548c\u5bf9\u5e94\u7684\u7ec4\u4ef6\u6216\u9875\u9762\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u989d\u5916\u7684\u5c5e\u6027\uff0c\u5982\u52a8\u6001\u8def\u7531\u53c2\u6570\u6216\u8def\u7531\u5b88\u536b\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528React Router\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u8def\u7531\u914d\u7f6e\uff1a<\/p>\n<pre><code>import { BrowserRouter as Router, Route, Switch } from &#039;react-router-dom&#039;;\nimport Home from &#039;.\/components\/Home&#039;;\nimport About from &#039;.\/components\/About&#039;;\nimport NotFound from &#039;.\/components\/NotFound&#039;;\n\nconst App = () =&gt; {\n  return (\n    &lt;Router&gt;\n      &lt;Switch&gt;\n        &lt;Route exact path=&quot;\/&quot; component={Home} \/&gt;\n        &lt;Route path=&quot;\/about&quot; component={About} \/&gt;\n        &lt;Route component={NotFound} \/&gt;\n      &lt;\/Switch&gt;\n    &lt;\/Router&gt;\n  );\n};\n\nexport default App;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u8bbf\u95ee\u4e0d\u540c\u7684\u8def\u5f84\u65f6\uff0c\u5bf9\u5e94\u7684\u7ec4\u4ef6\u5c31\u4f1a\u88ab\u6e32\u67d3\u5230\u9875\u9762\u4e0a\u3002<\/p>\n<p><strong>Question 2: \u600e\u6837\u5728JavaScript\u5e94\u7528\u4e2d\u5b9e\u73b0\u8def\u7531\u53c2\u6570\u7684\u4f20\u9012\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u5728JavaScript\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8def\u7531\u53c2\u6570\u6765\u4f20\u9012\u52a8\u6001\u7684\u503c\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u5b9e\u73b0\u6839\u636e\u4e0d\u540c\u7684\u53c2\u6570\u663e\u793a\u4e0d\u540c\u7684\u5185\u5bb9\u6216\u6570\u636e\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528Vue Router\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\u4e00\u4e2a\u5e26\u6709\u52a8\u6001\u53c2\u6570\u7684\u8def\u7531\uff1a<\/p>\n<pre><code class=\"language-javascript\">const router = new VueRouter({\n  routes: [\n    { path: &#039;\/user\/:id&#039;, component: User }\n  ]\n});\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5e26\u6709:id\u53c2\u6570\u7684\u8def\u7531\u3002\u5f53\u7528\u6237\u8bbf\u95ee\/user\/1\u65f6\uff0c\u4f1a\u6e32\u67d3User\u7ec4\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7props\u83b7\u53d6\u5230id\u7684\u503c\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728\u7ec4\u4ef6\u4e2d\u901a\u8fc7$route\u5bf9\u8c61\u6765\u83b7\u53d6\u8def\u7531\u53c2\u6570\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5728User\u7ec4\u4ef6\u4e2d\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-javascript\">export default {\n  mounted() {\n    const userId = this.$route.params.id;\n    \/\/ \u4f7f\u7528userId\u6765\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u6216\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\n  }\n}\n<\/code><\/pre>\n<p>\u5f53\u7528\u6237\u8bbf\u95ee\/user\/1\u65f6\uff0cmounted\u751f\u547d\u5468\u671f\u94a9\u5b50\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u83b7\u53d6\u5230id\u4e3a1\u7684\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<p><strong>Question 3: \u5728JavaScript\u5e94\u7528\u4e2d\u5982\u4f55\u5b9e\u73b0\u8def\u7531\u5bfc\u822a\u5b88\u536b\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8def\u7531\u5bfc\u822a\u5b88\u536b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u8fdb\u884c\u8def\u7531\u5bfc\u822a\u4e4b\u524d\u6216\u4e4b\u540e\u6267\u884c\u4e00\u4e9b\u903b\u8f91\u3002\u8fd9\u5728\u5904\u7406\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u3001\u6743\u9650\u63a7\u5236\u6216\u8df3\u8f6c\u903b\u8f91\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u5728\u4f7f\u7528React Router\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8def\u7531\u7684render\u5c5e\u6027\u6765\u5b9a\u4e49\u5bfc\u822a\u5b88\u536b\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u9700\u8981\u7528\u6237\u767b\u5f55\u7684\u79c1\u6709\u8def\u7531\uff1a<\/p>\n<pre><code class=\"language-javascript\">import { BrowserRouter as Router, Route, Redirect } from &#039;react-router-dom&#039;;\n\nconst PrivateRoute = ({ component: Component, ...rest }) =&gt; (\n  &lt;Route {...rest} render={props =&gt; (\n    isAuthenticated ? (\n      &lt;Component {...props} \/&gt;\n    ) : (\n      &lt;Redirect to=&quot;\/login&quot; \/&gt;\n    )\n  )} \/&gt;\n);\n\nconst App = () =&gt; {\n  return (\n    &lt;Router&gt;\n      &lt;Switch&gt;\n        &lt;PrivateRoute path=&quot;\/dashboard&quot; component={Dashboard} \/&gt;\n        &lt;Route path=&quot;\/login&quot; component={Login} \/&gt;\n        &lt;Route component={NotFound} \/&gt;\n      &lt;\/Switch&gt;\n    &lt;\/Router&gt;\n  );\n};\n\nexport default App;\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cPrivateRoute\u7ec4\u4ef6\u4f1a\u5224\u65ad\u7528\u6237\u662f\u5426\u5df2\u7ecf\u767b\u5f55\uff0c\u5982\u679c\u5df2\u7ecf\u767b\u5f55\u5219\u5c55\u793aDashboard\u7ec4\u4ef6\uff0c\u5426\u5219\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u9875\u9762\u3002<\/p>\n<p>\u8fd9\u6837\uff0c\u901a\u8fc7\u5b9a\u4e49\u8def\u7531\u5bfc\u822a\u5b88\u536b\uff0c\u4f60\u53ef\u4ee5\u5728\u8fdb\u884c\u8def\u7531\u5bfc\u822a\u4e4b\u524d\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u6216\u5176\u4ed6\u903b\u8f91\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8def\u7531\u4e3b\u8981\u6d89\u53ca\u5230\u4e86\u51e0\u4e2a\u5173\u952e\u70b9\uff1a\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u3001\u76d1\u542cURL\u53d8\u5316\u3001\u5339\u914d\u8def\u7531\u89c4\u5219\u3001\u6e32\u67d3 [&hellip;]","protected":false},"author":3,"featured_media":91326,"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\/91325"}],"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=91325"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/91325\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/91326"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=91325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=91325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=91325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}