{"id":309,"date":"2021-09-27T20:08:27","date_gmt":"2021-09-27T12:08:27","guid":{"rendered":"http:\/\/gjweb.top\/?p=309"},"modified":"2021-09-27T21:06:19","modified_gmt":"2021-09-27T13:06:19","slug":"react%e8%b7%af%e7%94%b1%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=309","title":{"rendered":"React\u8def\u7531\u4ecb\u7ecd"},"content":{"rendered":"\n<p>\u73b0\u4ee3\u7684\u524d\u7aef\u5e94\u7528\u5927\u591a\u6570\u90fd\u662f SPA (\u5355\u9875\u5e94\u7528\u7a0b\u5e8f),\u4e5f\u5c31\u662f\u53ea\u6709\u4e00\u4e2aHTML\u9875\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u56e0\u4e3a\u4ed6\u7684\u7528\u6237\u4f53\u9a8c\u66f4\u597d\u3001\u5bf9\u670d\u52a1\u5668\u7684\u538b\u529b\u4e5f\u66f4\u5c0f\u3002\u6240\u4ee5\u524d\u7aef\u8def\u7531\u662f\u4e3a\u4e86\u66f4\u6709\u6548\u7684\u4f7f\u7528\u5355\u4e2a\u9875\u9762\u6765\u7ba1\u7406\u539f\u6765\u591a\u9875\u9762\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u8def\u7531\u7684\u529f\u80fd\uff1a\u8ba9\u7528\u6237\u4ece\u4e00\u4e2a\u89c6\u56fe\u5bfc\u822a\u5230\u53e6\u4e00\u4e2a\u8bd5\u56fe\uff08\u9875\u9762\uff09<\/li><li>\u524d\u7aef\u8def\u7531\u662f\u4e00\u5957\u6620\u5c04\u89c4\u5219\uff0c\u5728React\u4e2d\uff0c\u65f6<strong>URL\u8def\u5f84<\/strong>\u4e0e<strong>\u7ec4\u4ef6<\/strong>\u7684\u5173\u7cfb<\/li><li>\u4f7f\u7528 React \u8def\u7531\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u914d\u7f6e <strong>\u8def\u5f84<\/strong> \u548c <strong>\u7ec4\u4ef6<\/strong> \uff08\u914d\u5bf9\uff09<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u4f7f\u7528<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5b89\u88c5<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add react-router-dom<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>\u5bfc\u5165\u8def\u7531\u7684\u4e09\u4e2a\u6838\u5fc3\u7ec4\u4ef6\uff1a <strong>Router \u3001 Route\u3001Link<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import {BroeserRputer as Router, Route, Link} from \"react-router-dom\"<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>\u4f7f\u7528 <strong>Router \u7ec4\u4ef6<\/strong> \u5305\u88f9\u6574\u4e2a\u5e94\u7528<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Router&gt;<br>    &lt;div&gt;<br> &nbsp;      ... \u7701\u7565\u9875\u9762\u5185\u5bb9<br> &nbsp; &nbsp;&lt;\/div&gt;<br>&lt;\/Router&gt;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>\u4f7f\u7528 <strong>Link \u7ec4\u4ef6<\/strong> \u4f5c\u4e3a\u5bfc\u822a\u83dc\u5355\uff08\u8def\u7531\u5165\u53e3\uff09<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Link to=\"\/first\"&gt;\u9875\u9762\u8df3\u8f6c&lt;\/Link&gt;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>\u4f7f\u7528 <strong>Router \u7ec4\u4ef6<\/strong> \u914d\u7f6e\u8def\u7531\u89c4\u5219\u548c\u8981\u5c55\u793a\u7684\u7ec4\u4ef6\uff08\u8def\u7531\u51fa\u53e3\uff09<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>const First = ()=&gt; &lt;p&gt;\u9875\u9762\u8df3\u8f6c&lt;\/p&gt;<br>&lt;Router&gt;<br>    &lt;div&gt;<br> &nbsp; &nbsp;    &lt;Link to=\"\/first\"&gt;\u9875\u9762&lt;\/Link&gt;<br>        &lt;Route path=\"\/first\" component={First}&gt;&lt;\/Route&gt;<br>    &lt;\/div&gt;<br>&lt;\/Router&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u7528\u7ec4\u4ef6\u8bf4\u660e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Router<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Router\u7ec4\u4ef6\uff1a\u5305\u88f9\u6574\u4e2a\u5e94\u7528\uff0c\u4e00\u4e2aReact\u5e94\u7528\u53ea\u9700\u8981<strong>\u4f7f\u7528\u4e00\u6b21<\/strong><\/li><li>\u4e24\u79cd\u5e38\u7528Router\uff1a <strong>HashRouter<\/strong> \u548c <strong>BroewserRouter<\/strong><\/li><li>HashRouter\uff1a\u4f7f\u7528 URL \u7684\u54c8\u5e0c\u503c\u5b9e\u73b0\uff08localhost:300\/#\/first\uff09<\/li><li><strong>\u63a8\u8350 BroewserRouter<\/strong> \uff1a\u4f7f\u7528 H5 \u7684 history API\u5b9e\u73b0<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import { BrowserRouter as Router, Route, Link } from \"react-router-dom\"<br>import { HashRouter as Router, Route, Link } from \"react-router-dom\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Link<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6307\u5b9a\u5bfc\u822a\u94fe\u63a5\uff08a\u5143\u7d20\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ to === href<br>&lt;Link to=\"\/first\"&gt;&lt;\/Link&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Route<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6307\u5b9a\u8def\u7531\u5c55\u793a\u7ec4\u4ef6\u76f8\u5173\u4fe1\u606f<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ path \u5c5e\u6027: \u8def\u7531\u89c4\u5219<br>\/\/ component \u5c5e\u6027: \u5c55\u793a\u7684\u7ec4\u4ef6<br>\/\/ Route \u7ec4\u4ef6\u5199\u5728\u54ea\u91cc,\u6e32\u67d3\u51fa\u6765\u7684\u7ec4\u4ef6\u5c31\u5c55\u793a\u5728\u54ea\u91cc<br>&lt;Route path=\"\/first\" component={First}&gt;&lt;\/Route&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f16\u7a0b\u5f0f\u5bfc\u822a<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u573a\u666f\uff1a\u70b9\u51fb\u767b\u5f55\u6309\u94ae\uff0c\u767b\u5f55\u6210\u529f\u540e\uff0c\u901a\u8fc7\u4ee3\u7801\u8df3\u8f6c\u5230\u540e\u53f0\u9996\u9875\uff0c\u5982\u4f55\u5b9e\u73b0<\/li><li>\u53d8\u7f16\u7a0b\u5f0f\u5bfc\u822a\uff1a\u901a\u8fc7JS\u4ee3\u7801\u5b9e\u73b0\u9875\u9762\u8df3\u8f6c<\/li><li>history \u662f React \u8def\u7531\u63d0\u4f9b\u7684\uff0c\u7528\u4e8e\u83b7\u53d6\u6d4f\u89c8\u5668\u5386\u53f2\u8bb0\u5f55\u7684\u76f8\u5173\u4fe1\u606f<\/li><li><strong>push(path)<\/strong> \uff1a\u8df3\u8f6c\u5230\u67d0\u4e2a\u9875\u9762\uff0c\u53c2\u6570 path \u8868\u793a\u8981\u8df3\u8f6c\u7684\u8def\u5f84<\/li><li><strong>go(n)<\/strong>\uff1a\u524d\u8fdb\u6216\u8005\u540e\u9000\u5230\u67d0\u4e2a\u9875\u9762\uff0c\u53c2\u6570 n \u8868\u793a\u524d\u8fdb\u6216\u8005\u9875\u9762\u6570\u91cf<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>const Login = (props)=&gt;{<br> &nbsp; &nbsp;const handleLogin = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;this.props.history.push(\".\/home\")<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5339\u914d\u6a21\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u7cca\u5339\u914d\u6a21\u5f0f<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u95ee\u9898\uff1a\u5f53 Link \u7ec4\u4ef6\u7684 to \u5c5e\u6027\u503c\u4e3a &#8220;\/login&#8221; \u65f6\uff0c\u4e3a\u4ec0\u4e48\u9ed8\u8ba4\u8def\u7531\u4e5f\u88ab\u5339\u914d\u6210\u529f\uff1f<\/li><li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cReact\u8def\u7531\u662f\u6a21\u7cca\u5339\u914d<\/li><li>\u6a21\u7cca\u5339\u914d\u89c4\u5219\uff1a\u53ea\u8981 <strong>pathname<\/strong> \u4ee5 <strong>path<\/strong> \u5f00\u5934\u5c31\u5339\u914d\u6210\u529f<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Link to=\"\/login\"&gt;\u767b\u5f55&lt;\/Link&gt;<br>&lt;Rout path=\"\/\" component={Home}\/&gt; \/\/\u5339\u914d\u6210\u529f<br>\/\/ path \u4ee3\u8868 Route \u7ec4\u4ef6\u7684 path \u5c5e\u6027<br>\/\/ pathname \u4ee3\u8868 Link \u7ec4\u4ef6\u7684 to \u5c5e\u6027 (location.pathname)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">path<\/th><th class=\"has-text-align-center\" data-align=\"center\">\u80fd\u591f\u5339\u914d\u7684 pathname<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\/<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6240\u6709 pathname<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">\/first<\/td><td class=\"has-text-align-center\" data-align=\"center\">\/first \u6216\u8005 \/first\/a \u6216\u8005 \/first\/a\/b\/&#8230;.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u7cbe\u786e\u5339\u914d<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u95ee\u9898\uff1a\u9ed8\u8ba4\u8def\u7531\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u5c55\u793a\uff0c\u5982\u679c\u907f\u514d\u8fd9\u79cd\u95ee\u9898?<\/li><li>\u7ed9 Route \u7ec4\u4ef6\u6dfb\u52a0 <strong>exact<\/strong> \u5c5e\u6027\uff0c\u8ba9\u5176\u53d8\u4e3a <strong>\u7cbe\u786e\u6a21\u5f0f<\/strong><\/li><li>\u7cbe\u786e\u5339\u914d\uff1a\u53ea\u6709\u5f53 <strong>path<\/strong> \u548c <strong>pathname<\/strong> \u5b8c\u5168\u5339\u914d\u65f6\u624d\u4f1a\u5c55\u793a\u8be5\u8def\u7531<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6b64\u65f6\uff0c\u8be5\u7ec4\u4ef6\u53ea\u80fd\u5339\u914d pathname=\"\/\"\u8fd9\u79cd\u60c5\u51b5<br>&lt;Route exact path=\"\/\" component={xxx}\/&gt;<\/code><\/pre>\n\n\n\n<p><strong>\u63a8\u8350\uff1a\u7ed9\u9ed8\u8ba4\u8def\u7531\u6dfb\u52a0 exact \u5c5e\u6027\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u4ee3\u7684\u524d\u7aef\u5e94\u7528\u5927\u591a\u6570\u90fd\u662f SPA (\u5355\u9875\u5e94\u7528\u7a0b\u5e8f),\u4e5f\u5c31\u662f\u53ea\u6709\u4e00\u4e2aHTML\u9875\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u56e0\u4e3a\u4ed6\u7684\u7528\u6237\u4f53\u9a8c\u66f4\u597d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=309"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}