{"id":91129,"date":"2026-08-07T00:40:45","date_gmt":"2026-08-06T16:40:45","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/91129.html"},"modified":"2026-08-07T00:40:45","modified_gmt":"2026-08-06T16:40:45","slug":"react-router-%e8%b7%af%e7%94%b1%e9%85%8d%e7%bd%ae%e8%80%81%e6%98%af%e8%b8%a9%e5%9d%91%ef%bc%9f7-%e7%a7%8d%e7%94%a8%e6%b3%95-3-%e4%b8%aa%e5%ae%9e%e6%88%98%e9%81%bf%e5%9d%91%ef%bc%8c%e4%b8%80%e6%ac%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/91129.html","title":{"rendered":"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f"},"content":{"rendered":"<p>\u505a SPA \u9879\u76ee&#xff0c;\u8def\u7531\u662f\u7ed5\u4e0d\u8fc7\u53bb\u7684\u4e00\u5173\u3002<\/p>\n<p>\u9875\u9762\u5207\u4e0d\u52a8\u3001\u767d\u5c4f\u5237\u65b0\u3001\u767b\u5f55\u540e\u8df3\u4e0d\u56de\u539f\u9875\u9762\u3001DataCloneError \u62a5\u9519\u2026\u2026 \u8fd9\u4e9b\u5751&#xff0c;\u57fa\u672c\u6bcf\u4e2a\u5199 React \u7684\u4eba\u90fd\u8e29\u8fc7\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u5e26\u4f60\u4ece\u96f6\u642d\u4e00\u5957\u5b8c\u6574\u53ef\u7528\u7684\u8def\u7531\u7cfb\u7edf&#xff1a;\u61d2\u52a0\u8f7d\u3001\u52a8\u6001\u8def\u7531\u3001\u5d4c\u5957\u8def\u7531\u3001\u91cd\u5b9a\u5411\u3001404 \u515c\u5e95\u3001\u9274\u6743\u62e6\u622a&#xff0c;\u5168\u90e8\u5b9e\u6218\u4ee3\u7801&#xff0c;\u590d\u5236\u5c31\u80fd\u8dd1\u3002<\/p>\n<p>\u770b\u5b8c\u4f60\u80fd\u89e3\u51b3\u4e09\u4ef6\u4e8b&#xff1a;<\/p>\n<ul>\n<li>\u641e\u61c2\u524d\u7aef\u8def\u7531\u5230\u5e95\u5728\u5e72\u4ec0\u4e48<\/li>\n<li>\u914d\u51fa\u4e00\u5957\u751f\u4ea7\u53ef\u7528\u7684\u8def\u7531\u65b9\u6848<\/li>\n<li>\u907f\u5f00\u9274\u6743\u8df3\u8f6c\u7684\u4e24\u4e2a\u7ecf\u5178\u5927\u5751 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260806164043-6a74b90b1b5b7.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u524d\u7aef\u8def\u7531<\/h3>\n<p>\u5148\u8bf4\u6e05\u695a\u4e00\u4ef6\u4e8b&#xff1a;\u4f20\u7edf\u8def\u7531\u662f\u540e\u7aef\u7684\u4e8b\u3002<\/p>\n<p>\u7528\u6237\u70b9\u4e2a\u94fe\u63a5&#xff0c;\u6d4f\u89c8\u5668\u53d1\u8bf7\u6c42&#xff0c;\u540e\u7aef\u8fd4\u56de\u65b0\u9875\u9762&#xff0c;\u5c4f\u5e55\u767d\u4e00\u4e0b&#xff0c;\u4f53\u9a8c\u62c9\u80ef\u3002<\/p>\n<p>\u524d\u540e\u7aef\u5206\u79bb\u540e&#xff0c;\u524d\u7aef\u63a5\u7ba1\u4e86\u9875\u9762\u5207\u6362&#xff0c;\u8fd9\u5c31\u662f SPA&#xff08;\u5355\u9875\u5e94\u7528&#xff09;\u3002<\/p>\n<p>\u6838\u5fc3\u601d\u8def\u4e00\u53e5\u8bdd&#xff1a;<\/p>\n<p>URL \u53d8\u4e86&#xff0c;\u4f46\u9875\u9762\u4e0d\u5237\u65b0&#xff0c;\u7531 JS \u6765\u5207\u6362\u663e\u793a\u7684\u7ec4\u4ef6\u3002<\/p>\n<p>React \u751f\u6001\u91cc&#xff0c;\u5e72\u8fd9\u4ef6\u4e8b\u7684\u5c31\u662f react-router-dom\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u8def\u7531\u9009\u578b&#xff1a;HashRouter \u8fd8\u662f BrowserRouter<\/h3>\n<p>\u8fd9\u4fe9\u90fd\u80fd\u7528&#xff0c;\u4f46\u539f\u7406\u4e0d\u540c\u3002<\/p>\n<h4>HashRouter<\/h4>\n<p>URL \u957f\u8fd9\u6837&#xff1a;http:\/\/localhost:5173\/#\/pay<\/p>\n<ul>\n<li>\u6539\u7684\u662f hash \u90e8\u5206&#xff08;# \u540e\u9762&#xff09;<\/li>\n<li>\u6539 hash \u4e0d\u4f1a\u5237\u65b0\u9875\u9762&#xff0c;\u76d1\u542c hashchange \u4e8b\u4ef6\u5c31\u884c<\/li>\n<li>\u7f3a\u70b9&#xff1a;URL \u6709\u70b9\u4e11<\/li>\n<\/ul>\n<h4>BrowserRouter<\/h4>\n<p>URL \u957f\u8fd9\u6837&#xff1a;http:\/\/localhost:5173\/pay<\/p>\n<ul>\n<li>\u8d70\u7684\u662f HTML5 \u7684 history API&#xff08;pushState \/ replaceState&#xff09;<\/li>\n<li>URL \u5e72\u51c0&#xff0c;\u7b26\u5408 RESTful \u98ce\u683c<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u90e8\u7f72\u65f6\u670d\u52a1\u7aef\u8981\u914d\u7f6e\u56de\u9000\u5230 index.html&#xff0c;\u5426\u5219\u5237\u65b0\u5c31 404<\/li>\n<\/ul>\n<p>\u672c\u9879\u76ee\u7528\u7684\u662f BrowserRouter&#xff1a;<\/p>\n<p>import { BrowserRouter as Router } from &#039;react-router-dom&#039;;<\/p>\n<p>&lt;Router&gt;<br \/>\n  {\/* \u5e94\u7528\u5185\u5bb9 *\/}<br \/>\n&lt;\/Router&gt;<\/p>\n<p>\u65b0\u624b\u5efa\u8bae&#xff1a;\u672c\u5730\u5f00\u53d1\u7528 BrowserRouter \u6ca1\u95ee\u9898&#xff0c;\u4e0a\u7ebf\u524d\u8bb0\u5f97\u914d nginx&#xff0c;\u4e0d\u7136\u5237\u65b0\u9875\u9762\u76f4\u63a5\u767d\u5c4f\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u8def\u7531\u914d\u7f6e\u5b9e\u6218&#xff1a;\u4e00\u5957\u914d\u9f50 7 \u79cd\u7528\u6cd5<\/h3>\n<p>\u76f4\u63a5\u770b\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6&#xff0c;\u6211\u628a\u6bcf\u79cd\u7528\u6cd5\u90fd\u6807\u4e86\u6ce8\u91ca&#xff1a;<\/p>\n<p>import { lazy, Suspense } from &#039;react&#039;;<br \/>\nimport {<br \/>\n  BrowserRouter as Router,<br \/>\n  Routes,<br \/>\n  Route,<br \/>\n  Navigate,<br \/>\n} from &#039;react-router-dom&#039;;<br \/>\nimport Navigation from &#039;.\/component\/Navigation&#039;;<br \/>\nimport ProtectRoute from &#039;.\/ProtectRoute&#039;;<br \/>\nimport Pay from &#039;.\/pages\/Pay&#039;;<\/p>\n<p>\/\/ 1. \u8def\u7531\u61d2\u52a0\u8f7d&#xff1a;\u6309\u9700\u52a0\u8f7d&#xff0c;\u63d0\u5347\u9996\u9875\u901f\u5ea6<br \/>\nconst Home &#061; lazy(() &#061;&gt; import(&#039;.\/pages\/Home&#039;));<br \/>\nconst About &#061; lazy(() &#061;&gt; import(&#039;.\/pages\/About&#039;));<br \/>\nconst User &#061; lazy(() &#061;&gt; import(&#039;.\/pages\/User&#039;));<br \/>\nconst NotFound &#061; lazy(() &#061;&gt; import(&#039;.\/pages\/NotFound&#039;));<br \/>\nconst Products &#061; lazy(() &#061;&gt; import(&#039;.\/Products&#039;));<br \/>\nconst ProductDetail &#061; lazy(() &#061;&gt; import(&#039;.\/Products\/ProductDetail&#039;));<br \/>\nconst NewProduct &#061; lazy(() &#061;&gt; import(&#039;.\/Products\/New&#039;));<br \/>\nconst Login &#061; lazy(() &#061;&gt; import(&#039;.\/pages\/Login&#039;));<\/p>\n<p>const App &#061; () &#061;&gt; {<br \/>\n  return (<br \/>\n    &lt;Router&gt;<br \/>\n      &lt;Suspense fallback&#061;{&lt;div&gt;\u7b49\u7b49\u6211\u5457&#8230;&lt;\/div&gt;}&gt;<br \/>\n        &lt;Navigation \/&gt;<br \/>\n        &lt;div id&#061;&#034;container&#034;&gt;<br \/>\n          &lt;Routes&gt;<br \/>\n            {\/* \u57fa\u7840\u8def\u7531 *\/}<br \/>\n            &lt;Route path&#061;&#034;\/&#034; element&#061;{&lt;Home \/&gt;} \/&gt;<br \/>\n            &lt;Route path&#061;&#034;\/about&#034; element&#061;{&lt;About \/&gt;} \/&gt;<\/p>\n<p>            {\/* 2. \u52a8\u6001\u8def\u7531&#xff1a;\u5192\u53f7\u5360\u4f4d *\/}<br \/>\n            &lt;Route path&#061;&#034;\/user\/:id&#034; element&#061;{&lt;User \/&gt;} \/&gt;<\/p>\n<p>            {\/* 3. \u5d4c\u5957\u8def\u7531&#xff1a;\u591a\u7ea7\u83dc\u5355 *\/}<br \/>\n            &lt;Route path&#061;&#034;\/products&#034; element&#061;{&lt;Products \/&gt;}&gt;<br \/>\n              &lt;Route path&#061;&#034;:productId&#034; element&#061;{&lt;ProductDetail \/&gt;} \/&gt;<br \/>\n              &lt;Route path&#061;&#034;new&#034; element&#061;{&lt;NewProduct \/&gt;} \/&gt;<br \/>\n            &lt;\/Route&gt;<\/p>\n<p>            {\/* 4. \u91cd\u5b9a\u5411&#xff1a;\u65e7\u8def\u5f84\u8df3\u65b0\u8def\u5f84 *\/}<br \/>\n            &lt;Route path&#061;&#034;\/old-path&#034; element&#061;{<br \/>\n              &lt;Navigate replace to&#061;&#034;\/products\/new&#034; \/&gt;<br \/>\n            } \/&gt;<\/p>\n<p>            {\/* 5. \u9274\u6743\u8def\u7531&#xff1a;\u5305\u88f9\u4e00\u5c42\u95e8\u7981 *\/}<br \/>\n            &lt;Route path&#061;&#034;\/login&#034; element&#061;{&lt;Login \/&gt;} \/&gt;<br \/>\n            &lt;Route path&#061;&#034;\/pay&#034; element&#061;{<br \/>\n              &lt;ProtectRoute&gt;<br \/>\n                &lt;Pay \/&gt;<br \/>\n              &lt;\/ProtectRoute&gt;<br \/>\n            } \/&gt;<\/p>\n<p>            {\/* 6. 404 \u515c\u5e95&#xff1a;* \u8d2a\u5a6a\u5339\u914d\u6240\u6709\u672a\u547d\u4e2d\u7684\u8def\u5f84 *\/}<br \/>\n            &lt;Route path&#061;&#034;*&#034; element&#061;{&lt;NotFound \/&gt;} \/&gt;<br \/>\n          &lt;\/Routes&gt;<br \/>\n        &lt;\/div&gt;<br \/>\n      &lt;\/Suspense&gt;<br \/>\n    &lt;\/Router&gt;<br \/>\n  );<br \/>\n};<\/p>\n<p>export default App;<\/p>\n<p>\u9010\u4e2a\u62c6\u5f00\u8bf4\u3002<\/p>\n<h4>1. \u61d2\u52a0\u8f7d&#xff1a;\u522b\u8ba9\u9996\u9875\u80cc\u5168\u91cf\u5305<\/h4>\n<p>lazy &#043; Suspense \u662f\u6027\u80fd\u4f18\u5316\u7684\u6807\u914d\u3002<\/p>\n<p>\u6ca1\u6709\u61d2\u52a0\u8f7d&#xff0c;\u6240\u6709\u9875\u9762\u6253\u5305\u8fdb\u4e00\u4e2a bundle&#xff0c;\u9996\u9875\u52a0\u8f7d\u6162\u5f97\u50cf\u8717\u725b\u3002<\/p>\n<p>\u7528\u4e86\u61d2\u52a0\u8f7d&#xff0c;\u8bbf\u95ee\u54ea\u4e2a\u9875\u9762\u624d\u52a0\u8f7d\u54ea\u4e2a chunk&#xff0c;\u9996\u9875\u4f53\u79ef\u76f4\u63a5\u7626\u4e0b\u6765\u3002<\/p>\n<p>Suspense \u7684 fallback \u662f\u52a0\u8f7d\u65f6\u7684\u5360\u4f4d&#xff0c;\u7ed9\u7528\u6237\u4e00\u4e2a&#034;\u6b63\u5728\u52a0\u8f7d&#034;\u7684\u53cd\u9988\u3002<\/p>\n<h4>2. \u52a8\u6001\u8def\u7531&#xff1a;\u4e00\u4e2a\u53c2\u6570\u641e\u5b9a\u8be6\u60c5\u9875<\/h4>\n<p>&lt;Route path&#061;&#034;\/user\/:id&#034; element&#061;{&lt;User \/&gt;} \/&gt;<\/p>\n<p>\/user\/123\u3001\/user\/456 \u5171\u7528\u540c\u4e00\u4e2a\u7ec4\u4ef6&#xff0c;\u7ec4\u4ef6\u91cc\u7528 useParams() \u53d6\u53c2\u6570&#xff1a;<\/p>\n<p>import { useParams } from &#039;react-router-dom&#039;;<br \/>\nconst User &#061; () &#061;&gt; {<br \/>\n  const { id } &#061; useParams();<br \/>\n  return &lt;div&gt;\u7528\u6237ID&#xff1a;{id}&lt;\/div&gt;;<br \/>\n};<\/p>\n<p>\u8be6\u60c5\u9875\u3001\u7f16\u8f91\u9875\u90fd\u9760\u8fd9\u4e00\u62db\u3002<\/p>\n<h4>3. \u5d4c\u5957\u8def\u7531&#xff1a;\u7236\u5b50\u7ed3\u6784\u6e05\u6670<\/h4>\n<p>\u4ea7\u54c1\u6a21\u5757\u4e0b\u6709\u5217\u8868\u3001\u8be6\u60c5\u3001\u65b0\u5efa&#xff0c;\u7528\u5d4c\u5957\u8def\u7531\u6700\u6e05\u6670&#xff1a;<\/p>\n<p>&lt;Route path&#061;&#034;\/products&#034; element&#061;{&lt;Products \/&gt;}&gt;<br \/>\n  &lt;Route path&#061;&#034;:productId&#034; element&#061;{&lt;ProductDetail \/&gt;} \/&gt;<br \/>\n  &lt;Route path&#061;&#034;new&#034; element&#061;{&lt;NewProduct \/&gt;} \/&gt;<br \/>\n&lt;\/Route&gt;<\/p>\n<p>\u7236\u7ec4\u4ef6 Products \u91cc\u7528 &lt;Outlet \/&gt; \u5360\u4f4d&#xff0c;\u5b50\u8def\u7531\u4f1a\u6e32\u67d3\u5728\u5360\u4f4d\u5904&#xff1a;<\/p>\n<p>import { Outlet } from &#039;react-router-dom&#039;;<br \/>\nconst Products &#061; () &#061;&gt; (<br \/>\n  &lt;div&gt;<br \/>\n    &lt;h1&gt;\u4ea7\u54c1\u5217\u8868&lt;\/h1&gt;<br \/>\n    &lt;Outlet \/&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n);<\/p>\n<h4>4. \u91cd\u5b9a\u5411&#xff1a;\u65e7\u8def\u5f84\u5e73\u6ed1\u8fc1\u79fb<\/h4>\n<p>&lt;Route path&#061;&#034;\/old-path&#034; element&#061;{&lt;Navigate replace to&#061;&#034;\/products\/new&#034; \/&gt;} \/&gt;<\/p>\n<p>replace \u8868\u793a\u66ff\u6362\u5386\u53f2\u8bb0\u5f55&#xff0c;\u7528\u6237\u70b9\u540e\u9000\u4e0d\u4f1a\u56de\u5230\u65e7\u8def\u5f84\u3002<\/p>\n<h4>5. 404 \u515c\u5e95<\/h4>\n<p>&lt;Route path&#061;&#034;*&#034; element&#061;{&lt;NotFound \/&gt;} \/&gt;<\/p>\n<p>* \u662f\u901a\u914d\u7b26&#xff0c;\u653e\u5728\u6700\u540e&#xff0c;\u524d\u9762\u6ca1\u5339\u914d\u4e0a\u7684\u5168\u5f52\u5b83\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001Link \u7ec4\u4ef6&#xff1a;\u522b\u7528 a \u6807\u7b7e<\/h3>\n<p>\u5bfc\u822a\u680f\u7528 Link&#xff0c;\u522b\u7528 &lt;a&gt;&#xff1a;<\/p>\n<p>import { Link } from &#039;react-router-dom&#039;;<\/p>\n<p>function Navigation() {<br \/>\n  return (<br \/>\n    &lt;nav&gt;<br \/>\n      &lt;ul&gt;<br \/>\n        &lt;li&gt;&lt;Link to&#061;&#034;\/&#034;&gt;Home&lt;\/Link&gt;&lt;\/li&gt;<br \/>\n        &lt;li&gt;&lt;Link to&#061;&#034;\/about&#034;&gt;About&lt;\/Link&gt;&lt;\/li&gt;<br \/>\n        &lt;li&gt;&lt;Link to&#061;&#034;\/user\/123&#034;&gt;User&lt;\/Link&gt;&lt;\/li&gt;<br \/>\n        &lt;li&gt;&lt;Link to&#061;&#034;\/products\/123&#034;&gt;\u4ea7\u54c1\u8be6\u60c5&lt;\/Link&gt;&lt;\/li&gt;<br \/>\n        &lt;li&gt;&lt;Link to&#061;&#034;\/pay&#034;&gt;\u652f\u4ed8&lt;\/Link&gt;&lt;\/li&gt;<br \/>\n      &lt;\/ul&gt;<br \/>\n    &lt;\/nav&gt;<br \/>\n  );<br \/>\n}<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u7528 &lt;a&gt;&#xff1f;<\/p>\n<p>\u56e0\u4e3a &lt;a&gt; \u4f1a\u89e6\u53d1\u6574\u9875\u5237\u65b0&#xff0c;SPA \u76f4\u63a5\u5e9f\u4e86\u3002<\/p>\n<p>Link \u5185\u90e8\u8c03\u7528 history.pushState&#xff0c;\u53ea\u6539 URL \u4e0d\u5237\u65b0&#xff0c;\u8fd9\u624d\u662f SPA \u8be5\u6709\u7684\u6837\u5b50\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001\u9274\u6743\u8def\u7531&#xff1a;\u672c\u6587\u7684\u91cd\u70b9<\/h3>\n<p>\u8fd9\u8282\u662f\u5168\u6587\u5e72\u8d27\u5bc6\u5ea6\u6700\u9ad8\u7684\u5730\u65b9&#xff0c;\u4e24\u4e2a\u5751\u6211\u6328\u4e2a\u8bb2\u3002<\/p>\n<h4>\u9700\u6c42\u573a\u666f<\/h4>\n<p>\u7528\u6237\u6ca1\u767b\u5f55\u5c31\u60f3\u8bbf\u95ee \/pay&#xff1f;\u62e6\u4e0b\u6765&#xff0c;\u8df3\u5230\u767b\u5f55\u9875\u3002<\/p>\n<p>\u767b\u5f55\u6210\u529f\u540e&#xff0c;\u81ea\u52a8\u8df3\u56de\u4ed6\u672c\u6765\u60f3\u53bb\u7684 \/pay&#xff0c;\u800c\u4e0d\u662f\u50bb\u4e4e\u4e4e\u5730\u8df3\u5230\u9996\u9875\u3002<\/p>\n<h4>\u95e8\u7981\u7ec4\u4ef6&#xff1a;ProtectRoute<\/h4>\n<p>import { Navigate, useLocation } from &#039;react-router-dom&#039;;<\/p>\n<p>const ProtectRoute &#061; ({ children }) &#061;&gt; {<br \/>\n  const isLogin &#061; localStorage.getItem(&#039;isLogin&#039;) &#061;&#061;&#061; &#039;true&#039;;<br \/>\n  const location &#061; useLocation();<\/p>\n<p>  if (!isLogin) {<br \/>\n    \/\/ \u628a&#034;\u4ece\u54ea\u6765&#034;\u7684\u4fe1\u606f\u585e\u8fdb state&#xff0c;\u767b\u5f55\u9875\u53d6\u51fa\u6765\u8df3\u56de\u53bb<br \/>\n    return &lt;Navigate to&#061;&#034;\/login&#034; replace state&#061;{{ from: location }} \/&gt;;<br \/>\n  }<\/p>\n<p>  return &lt;div&gt;{children}&lt;\/div&gt;;<br \/>\n};<\/p>\n<p>\u8fd9\u91cc\u7684\u5173\u952e\u662f children\u3002<\/p>\n<p>children \u662f React \u7684&#034;\u63d2\u69fd&#034;\u673a\u5236\u2014\u2014\u7236\u7ec4\u4ef6\u5305\u88f9\u7684\u5b50\u8282\u70b9&#xff0c;\u4f1a\u4f5c\u4e3a props.children \u4f20\u8fdb\u6765\u3002<\/p>\n<p>&lt;ProtectRoute&gt;<br \/>\n  &lt;Pay \/&gt;   {\/* \u8fd9\u5c31\u662f children *\/}<br \/>\n&lt;\/ProtectRoute&gt;<\/p>\n<p>\u8fd9\u79cd\u6a21\u5f0f\u8ba9 ProtectRoute \u53d8\u6210\u53ef\u590d\u7528\u7684\u95e8\u7981&#xff1a;\u4efb\u4f55\u9700\u8981\u9274\u6743\u7684\u9875\u9762&#xff0c;\u5957\u4e00\u5c42\u5c31\u884c&#xff0c;\u4e0d\u7528\u6539\u539f\u7ec4\u4ef6\u3002<\/p>\n<h4>\u767b\u5f55\u9875&#xff1a;\u53d6\u503c\u8df3\u56de<\/h4>\n<p>import { useNavigate, useLocation } from &#039;react-router-dom&#039;;<\/p>\n<p>const Login &#061; () &#061;&gt; {<br \/>\n  const navigate &#061; useNavigate();<br \/>\n  const location &#061; useLocation();<\/p>\n<p>  \/\/ \u94fe\u5f0f\u53d6\u503c&#xff0c;\u62ff\u4e0d\u5230\u5c31\u56de\u9000\u9996\u9875<br \/>\n  const from &#061; location.state?.from?.pathname || &#039;\/&#039;;<\/p>\n<p>  function handleSubmit(e) {<br \/>\n    e.preventDefault();<br \/>\n    const formData &#061; new FormData(e.currentTarget);<br \/>\n    const username &#061; formData.get(&#039;username&#039;);<br \/>\n    const password &#061; formData.get(&#039;password&#039;);<\/p>\n<p>    if (username &#061;&#061;&#061; &#039;admin&#039; &amp;&amp; password &#061;&#061;&#061; &#039;123456&#039;) {<br \/>\n      localStorage.setItem(&#039;isLogin&#039;, &#039;true&#039;);<br \/>\n      navigate(from, { replace: true });<br \/>\n    } else {<br \/>\n      alert(&#039;\u767b\u5f55\u5931\u8d25&#039;);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  return (<br \/>\n    &lt;form onSubmit&#061;{handleSubmit}&gt;<br \/>\n      &lt;h1&gt;\u767b\u5f55\u9875&lt;\/h1&gt;<br \/>\n      &lt;input type&#061;&#034;text&#034; name&#061;&#034;username&#034; placeholder&#061;&#034;\u8bf7\u8f93\u5165\u7528\u6237\u540d&#034; required \/&gt;<br \/>\n      &lt;input type&#061;&#034;password&#034; name&#061;&#034;password&#034; placeholder&#061;&#034;\u8bf7\u8f93\u5165\u5bc6\u7801&#034; required \/&gt;<br \/>\n      &lt;button type&#061;&#034;submit&#034;&gt;\u767b\u5f55&lt;\/button&gt;<br \/>\n    &lt;\/form&gt;<br \/>\n  );<br \/>\n};<\/p>\n<h4>\u8fd9\u884c\u4ee3\u7801\u662f\u6574\u6761\u94fe\u7684\u7075\u9b42<\/h4>\n<p><span class=\"token keyword\">const<\/span> from <span class=\"token operator\">&#061;<\/span> location<span class=\"token punctuation\">.<\/span>state<span class=\"token operator\">?.<\/span>from<span class=\"token operator\">?.<\/span>pathname <span class=\"token operator\">||<\/span> <span class=\"token string\">&#039;\/&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u62c6\u5f00\u770b&#xff1a;<\/p>\n<table>\n<tr>\u8868\u8fbe\u5f0f\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>location.state<\/td>\n<td>\/login \u8def\u7531\u4e0a\u6302\u7684 state<\/td>\n<\/tr>\n<tr>\n<td>location.state?.from<\/td>\n<td>ProtectRoute \u4f20\u8fc7\u6765\u7684\u539f location \u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>?.from?.pathname<\/td>\n<td>\u53d6\u5b83\u7684 pathname&#xff0c;\u5373 \/pay<\/td>\n<\/tr>\n<tr>\n<td>&#096;<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>?. \u53ef\u9009\u94fe\u7684\u4f5c\u7528&#xff1a;\u76f4\u63a5\u8bbf\u95ee \/login \u65f6 state \u662f undefined&#xff0c;\u6ca1\u6709 ?. \u4f1a\u76f4\u63a5\u62a5 TypeError\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001\u8e29\u5751\u5b9e\u5f55&#xff1a;\u4e24\u4e2a\u5751\u6211\u66ff\u4f60\u8e29\u8fc7\u4e86<\/h3>\n<h4>\u5751 1&#xff1a;DataCloneError: Location object could not be cloned<\/h4>\n<p>\u62a5\u9519\u73b0\u573a&#xff1a;<\/p>\n<p>Uncaught DataCloneError: Failed to execute &#039;replaceState&#039; on &#039;History&#039;:<br \/>\nLocation object could not be cloned.<\/p>\n<p>\u9519\u8bef\u5199\u6cd5&#xff1a;<\/p>\n<p>\/\/ \u76f4\u63a5\u7528\u4e86\u5168\u5c40 window.location&#xff0c;\u5b83\u662f DOM \u5bf9\u8c61<br \/>\nreturn &lt;Navigate to&#061;&#034;\/login&#034; replace state&#061;{{ from: location }} \/&gt;<\/p>\n<p>\u8fd9\u91cc\u7684 location \u662f window.location&#xff0c;\u662f\u6d4f\u89c8\u5668\u5bbf\u4e3b\u5bf9\u8c61\u3002<\/p>\n<p>React Router \u5185\u90e8\u4f1a\u628a state \u4f20\u7ed9 history.replaceState()&#xff0c;\u6d4f\u89c8\u5668\u7528\u7ed3\u6784\u5316\u514b\u9686\u7b97\u6cd5\u5e8f\u5217\u5316\u5b83\u3002\u800c DOM Location \u5bf9\u8c61\u65e0\u6cd5\u88ab\u514b\u9686&#xff0c;\u4e8e\u662f\u629b\u9519\u3002<\/p>\n<p>\u6b63\u786e\u5199\u6cd5&#xff1a;\u7528 useLocation() \u62ff\u5230\u7684\u662f\u666e\u901a\u5bf9\u8c61&#xff0c;\u53ef\u514b\u9686&#xff1a;<\/p>\n<p>const location &#061; useLocation();<br \/>\nreturn &lt;Navigate to&#061;&#034;\/login&#034; replace state&#061;{{ from: location }} \/&gt;<\/p>\n<p>\u8bb0\u4f4f\u4e00\u53e5\u8bdd&#xff1a;\u8def\u7531\u76f8\u5173\u7684\u4e1c\u897f&#xff0c;\u6c38\u8fdc\u7528 react-router-dom \u63d0\u4f9b\u7684 hook&#xff0c;\u522b\u78b0 window.location\u3002<\/p>\n<h4>\u5751 2&#xff1a;\u767b\u5f55\u540e\u56de\u4e0d\u5230\u539f\u9875\u9762&#xff0c;\u8df3\u5230\u4e86\u9996\u9875<\/h4>\n<p>\u9519\u8bef\u73b0\u573a&#xff1a;\u4ece \/pay \u88ab\u62e6\u5230 \/login&#xff0c;\u767b\u5f55\u6210\u529f\u540e\u5374\u8df3\u5230\u4e86 \/\u3002<\/p>\n<p>\u539f\u56e0&#xff1a;\u4e24\u8fb9\u6570\u636e\u683c\u5f0f\u5bf9\u4e0d\u4e0a\u3002<\/p>\n<p>\/\/ ProtectRoute \u4f20\u7684\u662f\u5b57\u7b26\u4e32<br \/>\nstate&#061;{{ from: location.href }}   \/\/ &#034;http:\/\/localhost:5173\/pay&#034;<\/p>\n<p>\/\/ Login \u5374\u6309\u5bf9\u8c61\u53d6<br \/>\nlocation.state?.from?.pathname    \/\/ \u5b57\u7b26\u4e32\u4e0a\u6ca1\u6709 pathname&#xff0c;\u7ed3\u679c\u662f undefined<\/p>\n<p>\u5b57\u7b26\u4e32\u4e0a\u6ca1\u6709 .pathname&#xff0c;\u53d6\u51fa\u6765\u662f undefined&#xff0c;|| &#039;\/&#039; \u515c\u5e95\u751f\u6548&#xff0c;\u4e8e\u662f\u8df3\u5230\u4e86\u9996\u9875\u3002<\/p>\n<p>\u4fee\u590d&#xff1a;\u4e24\u8fb9\u7edf\u4e00\u6210\u5bf9\u8c61\u5f62\u6001\u3002<\/p>\n<p>\/\/ ProtectRoute<br \/>\nstate&#061;{{ from: location }}   \/\/ \u4f20 useLocation() \u7684\u5bf9\u8c61<\/p>\n<p>\/\/ Login<br \/>\nlocation.state?.from?.pathname   \/\/ \u6b63\u786e\u53d6\u5230 &#034;\/pay&#034;<\/p>\n<p>\u907f\u5751\u539f\u5219&#xff1a;\u4f20\u503c\u548c\u53d6\u503c\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u4e25\u683c\u5bf9\u5e94&#xff0c;\u8de8\u7ec4\u4ef6\u4f20\u5bf9\u8c61\u65f6\u5c24\u5176\u8981\u786e\u8ba4\u5b57\u6bb5\u540d\u4e00\u81f4\u3002<\/p>\n<h4>\u5751 3&#xff1a;FormData \u53d6\u4e0d\u5230\u503c<\/h4>\n<p>\u8fd9\u662f\u4e2a\u9690\u85cf\u5751&#xff0c;\u65b0\u624b\u5f88\u5bb9\u6613\u4e2d\u62db\u3002<\/p>\n<p>\u9519\u8bef\u5199\u6cd5&#xff1a;<\/p>\n<p>&lt;input type&#061;&#034;text&#034; placeholder&#061;&#034;\u8bf7\u8f93\u5165\u7528\u6237\u540d&#034; required \/&gt;<br \/>\n&lt;input type&#061;&#034;password&#034; placeholder&#061;&#034;\u8bf7\u8f93\u5165\u5bc6\u7801&#034; required \/&gt;<\/p>\n<p>new FormData(form) \u53ea\u6536\u96c6\u5e26 name \u5c5e\u6027\u7684\u8868\u5355\u63a7\u4ef6\u3002\u4e0a\u9762\u4e24\u4e2a input \u6ca1\u5199 name&#xff0c;formData.get(&#039;username&#039;) \u6c38\u8fdc\u8fd4\u56de null&#xff0c;\u767b\u5f55\u6c38\u8fdc\u5931\u8d25\u3002<\/p>\n<p>\u6b63\u786e\u5199\u6cd5&#xff1a;<\/p>\n<p>&lt;input type&#061;&#034;text&#034; name&#061;&#034;username&#034; placeholder&#061;&#034;\u8bf7\u8f93\u5165\u7528\u6237\u540d&#034; required \/&gt;<br \/>\n&lt;input type&#061;&#034;password&#034; name&#061;&#034;password&#034; placeholder&#061;&#034;\u8bf7\u8f93\u5165\u5bc6\u7801&#034; required \/&gt;<\/p>\n<p>\u8bb0\u4f4f&#xff1a;\u7528 FormData&#xff0c;input \u5fc5\u987b\u6709 name&#xff0c;\u8fd9\u662f HTML \u8868\u5355\u7684\u57fa\u7840\u89c4\u5219&#xff0c;\u8ddf React \u65e0\u5173\u3002<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u8def\u7531\u5bf9\u8c61\u901f\u67e5\u8868<\/h3>\n<table>\n<tr>\u5bf9\u8c61\/Hook\u4f5c\u7528\u5178\u578b\u7528\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>useNavigate<\/td>\n<td>\u4ee3\u7801\u91cc\u4e3b\u52a8\u8df3\u8f6c<\/td>\n<td>navigate(&#039;\/pay&#039;, { replace: true })<\/td>\n<\/tr>\n<tr>\n<td>useLocation<\/td>\n<td>\u62ff\u5f53\u524d\u8def\u7531\u4fe1\u606f<\/td>\n<td>location.pathname\u3001location.state<\/td>\n<\/tr>\n<tr>\n<td>useParams<\/td>\n<td>\u53d6\u52a8\u6001\u8def\u7531\u53c2\u6570<\/td>\n<td>const { id } &#061; useParams()<\/td>\n<\/tr>\n<tr>\n<td>Link<\/td>\n<td>\u58f0\u660e\u5f0f\u8df3\u8f6c<\/td>\n<td>&lt;Link to&#061;&#034;\/about&#034;&gt;\u5173\u4e8e&lt;\/Link&gt;<\/td>\n<\/tr>\n<tr>\n<td>Navigate<\/td>\n<td>\u7ec4\u4ef6\u5f0f\u91cd\u5b9a\u5411<\/td>\n<td>&lt;Navigate to&#061;&#034;\/login&#034; replace \/&gt;<\/td>\n<\/tr>\n<tr>\n<td>Outlet<\/td>\n<td>\u5d4c\u5957\u8def\u7531\u5360\u4f4d<\/td>\n<td>\u7236\u7ec4\u4ef6\u91cc\u6e32\u67d3\u5b50\u8def\u7531<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>navigate \u5bf9\u5e94\u7f16\u7a0b\u5f0f\u5bfc\u822a&#xff0c;Link \u5bf9\u5e94\u58f0\u660e\u5f0f\u5bfc\u822a&#xff0c;\u6309\u573a\u666f\u9009\u3002<\/p>\n<hr \/>\n<h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<p>\u4e00\u56fe\u770b\u61c2\u6574\u5957\u9274\u6743\u6d41\u7a0b&#xff1a;<\/p>\n<li>\u7528\u6237\u8bbf\u95ee \/pay<\/li>\n<li>ProtectRoute \u68c0\u67e5 localStorage&#xff0c;\u6ca1\u767b\u5f55<\/li>\n<li>Navigate \u8df3 \/login&#xff0c;\u628a\u5f53\u524d location \u585e\u8fdb state.from<\/li>\n<li>\u767b\u5f55\u9875 useLocation \u53d6\u51fa from.pathname<\/li>\n<li>\u767b\u5f55\u6210\u529f&#xff0c;navigate(from, { replace: true }) \u8df3\u56de \/pay<\/li>\n<p>\u4e09\u4e2a\u6838\u5fc3\u539f\u5219&#xff1a;<\/p>\n<ul>\n<li>\u8def\u7531 state \u53ea\u653e\u53ef\u5e8f\u5217\u5316\u7684\u666e\u901a\u5bf9\u8c61&#xff0c;\u522b\u653e DOM \u5bf9\u8c61<\/li>\n<li>\u4f20\u503c\u548c\u53d6\u503c\u7684\u5b57\u6bb5\u7ed3\u6784\u8981\u4e25\u683c\u5bf9\u5e94<\/li>\n<li>\u8868\u5355\u63a7\u4ef6\u5fc5\u987b\u52a0 name&#xff0c;FormData \u624d\u80fd\u6536\u96c6\u5230\u503c<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u505a SPA \u9879\u76ee&#xff0c;\u8def\u7531\u662f\u7ed5\u4e0d\u8fc7\u53bb\u7684\u4e00\u5173\u3002<br \/>\n\u9875\u9762\u5207\u4e0d\u52a8\u3001\u767d\u5c4f\u5237\u65b0\u3001\u767b\u5f55\u540e\u8df3\u4e0d\u56de\u539f\u9875\u9762\u3001DataCloneError \u62a5\u9519\u2026\u2026 \u8fd9\u4e9b\u5751&#xff0c;\u57fa\u672c\u6bcf\u4e2a\u5199 React \u7684\u4eba\u90fd\u8e29\u8fc7\u3002<br \/>\n\u8fd9\u7bc7\u6587\u7ae0\u5e26\u4f60\u4ece\u96f6\u642d\u4e00\u5957\u5b8c\u6574\u53ef\u7528\u7684\u8def\u7531\u7cfb\u7edf&#xff1a;\u61d2\u52a0\u8f7d\u3001\u52a8\u6001\u8def\u7531\u3001\u5d4c\u5957\u8def\u7531\u3001\u91cd\u5b9a\u5411\u3001404 \u515c\u5e95\u3001\u9274\u6743\u62e6\u622a&#xff0c;\u5168\u90e8\u5b9e\u6218\u4ee3\u7801&#xff0c;\u590d\u5236\u5c31\u80fd\u8dd1\u3002<br \/>\n\u770b\u5b8c\u4f60\u80fd\u89e3\u51b3\u4e09\u4ef6\u4e8b&#xff1a;<br \/>\n\u641e\u61c2\u524d\u7aef\u8def\u7531\u5230\u5e95\u5728\u5e72\u4ec0\u4e48\u914d\u51fa\u4e00\u5957\u751f\u4ea7\u53ef\u7528\u7684\u8def\u7531\u65b9\u6848\u907f\u5f00\u9274\u6743\u8df3\u8f6c\u7684\u4e24<\/p>\n","protected":false},"author":2,"featured_media":91128,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10336,524,87,885],"topic":[],"class_list":["post-91129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-10336","tag-react-js","tag-87","tag-885"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/91129.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u505a SPA \u9879\u76ee&#xff0c;\u8def\u7531\u662f\u7ed5\u4e0d\u8fc7\u53bb\u7684\u4e00\u5173\u3002 \u9875\u9762\u5207\u4e0d\u52a8\u3001\u767d\u5c4f\u5237\u65b0\u3001\u767b\u5f55\u540e\u8df3\u4e0d\u56de\u539f\u9875\u9762\u3001DataCloneError \u62a5\u9519\u2026\u2026 \u8fd9\u4e9b\u5751&#xff0c;\u57fa\u672c\u6bcf\u4e2a\u5199 React \u7684\u4eba\u90fd\u8e29\u8fc7\u3002 \u8fd9\u7bc7\u6587\u7ae0\u5e26\u4f60\u4ece\u96f6\u642d\u4e00\u5957\u5b8c\u6574\u53ef\u7528\u7684\u8def\u7531\u7cfb\u7edf&#xff1a;\u61d2\u52a0\u8f7d\u3001\u52a8\u6001\u8def\u7531\u3001\u5d4c\u5957\u8def\u7531\u3001\u91cd\u5b9a\u5411\u3001404 \u515c\u5e95\u3001\u9274\u6743\u62e6\u622a&#xff0c;\u5168\u90e8\u5b9e\u6218\u4ee3\u7801&#xff0c;\u590d\u5236\u5c31\u80fd\u8dd1\u3002 \u770b\u5b8c\u4f60\u80fd\u89e3\u51b3\u4e09\u4ef6\u4e8b&#xff1a; \u641e\u61c2\u524d\u7aef\u8def\u7531\u5230\u5e95\u5728\u5e72\u4ec0\u4e48\u914d\u51fa\u4e00\u5957\u751f\u4ea7\u53ef\u7528\u7684\u8def\u7531\u65b9\u6848\u907f\u5f00\u9274\u6743\u8df3\u8f6c\u7684\u4e24\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/91129.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-06T16:40:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260806164043-6a74b90b1b5b7.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/91129.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/91129.html\",\"name\":\"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-06T16:40:45+00:00\",\"dateModified\":\"2026-08-06T16:40:45+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/91129.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/91129.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/91129.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/91129.html","og_locale":"zh_CN","og_type":"article","og_title":"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u505a SPA \u9879\u76ee&#xff0c;\u8def\u7531\u662f\u7ed5\u4e0d\u8fc7\u53bb\u7684\u4e00\u5173\u3002 \u9875\u9762\u5207\u4e0d\u52a8\u3001\u767d\u5c4f\u5237\u65b0\u3001\u767b\u5f55\u540e\u8df3\u4e0d\u56de\u539f\u9875\u9762\u3001DataCloneError \u62a5\u9519\u2026\u2026 \u8fd9\u4e9b\u5751&#xff0c;\u57fa\u672c\u6bcf\u4e2a\u5199 React \u7684\u4eba\u90fd\u8e29\u8fc7\u3002 \u8fd9\u7bc7\u6587\u7ae0\u5e26\u4f60\u4ece\u96f6\u642d\u4e00\u5957\u5b8c\u6574\u53ef\u7528\u7684\u8def\u7531\u7cfb\u7edf&#xff1a;\u61d2\u52a0\u8f7d\u3001\u52a8\u6001\u8def\u7531\u3001\u5d4c\u5957\u8def\u7531\u3001\u91cd\u5b9a\u5411\u3001404 \u515c\u5e95\u3001\u9274\u6743\u62e6\u622a&#xff0c;\u5168\u90e8\u5b9e\u6218\u4ee3\u7801&#xff0c;\u590d\u5236\u5c31\u80fd\u8dd1\u3002 \u770b\u5b8c\u4f60\u80fd\u89e3\u51b3\u4e09\u4ef6\u4e8b&#xff1a; \u641e\u61c2\u524d\u7aef\u8def\u7531\u5230\u5e95\u5728\u5e72\u4ec0\u4e48\u914d\u51fa\u4e00\u5957\u751f\u4ea7\u53ef\u7528\u7684\u8def\u7531\u65b9\u6848\u907f\u5f00\u9274\u6743\u8df3\u8f6c\u7684\u4e24","og_url":"https:\/\/www.wsisp.com\/helps\/91129.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-06T16:40:45+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260806164043-6a74b90b1b5b7.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/91129.html","url":"https:\/\/www.wsisp.com\/helps\/91129.html","name":"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-06T16:40:45+00:00","dateModified":"2026-08-06T16:40:45+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/91129.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/91129.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/91129.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"React Router \u8def\u7531\u914d\u7f6e\u8001\u662f\u8e29\u5751\uff1f7 \u79cd\u7528\u6cd5 + 3 \u4e2a\u5b9e\u6218\u907f\u5751\uff0c\u4e00\u6b21\u6027\u8bb2\u900f"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/91129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=91129"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/91129\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/91128"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=91129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=91129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=91129"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=91129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}