{"id":112,"date":"2021-02-05T21:57:39","date_gmt":"2021-02-05T13:57:39","guid":{"rendered":"http:\/\/qqiuklele.cn\/?p=112"},"modified":"2025-03-26T15:20:37","modified_gmt":"2025-03-26T07:20:37","slug":"%e9%82%a3%e4%ba%9b%e6%88%91%e5%9c%a8%e5%8d%9a%e5%ae%a2%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%8d%a1%e5%a3%b3%e7%9a%84%e5%9d%91","status":"publish","type":"post","link":"https:\/\/qqiukulele.cn\/?p=112","title":{"rendered":"\u90a3\u4e9b\u6211\u5728\u535a\u5ba2\u9879\u76ee\u4e2d\u5361\u58f3\u7684\u5751"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Next.js\uff0810.0.4\uff09\u5f15\u5165antd\u7ec4\u4ef6<\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u524d\u7aef\u9875\u9762\u5f00\u53d1\u6211\u4f7f\u7528\u4e86<strong>nextJS\u6846\u67b6<\/strong>\u6784\u5efa\u5e94\u7528\uff0c\u4f46\u662f\u6211\u6574\u4e86\u4e00\u4e2a\u4e0a\u5348\u90fd\u6ca1\u914d\u7f6e\u51fa\u6765\uff0c\u641e\u5f97\u6211\u4e0b\u5348\u5b66\u8f66\u5fc3\u4e0d\u5728\u7109\u5509\uff0c\u665a\u4e0a\u56de\u5bb6\u4e00\u8bd5\uff0c\u5c31\u6b27\u514b\u4e86\uff01\u8d76\u7d27\u603b\u7ed3\u4e00\u4e0b\uff01<\/p>\n\n\n\n<p>\u7531\u4e8e\u4ee5\u524dNext.js\u662f\u4e0d\u652f\u6301\u5728\u6587\u4ef6\u91ccimport\u6837\u5f0f\u7684\uff0c\u6240\u4ee5\u6211\u5728\u7f51\u4e0a\u67e5\u627e\u7684\u5f88\u591a\u65b9\u6cd5\u662f\u901a\u8fc7\u4e0b\u8f7d<code>@zeit\/next-css<\/code>\u5305\u6765\u8ba9Next\u652f\u6301CSS\u6587\u4ef6\u3002\u4e0d\u8fc7\uff0c\u770b<code>@zeit\/next-css<\/code>\u5305\u7684npm\u6587\u6863\u4ee5\u53ca<a href=\"https:\/\/www.nextjs.cn\/learn\/basics\/assets-metadata-css\/layout-component\">[<strong>Next\u6587\u6863<\/strong>] <\/a>\u77e5\u9053\uff0c\u73b0\u5728\u5df2\u7ecf\u4e0d\u9700\u8981\u8fd9\u4e48\u9ebb\u70e6\u4e86\uff0c\u800c\u662f\u76f4\u63a5\u53ef\u4ee5\u5728js\u6587\u4ef6\u91ccimport\u3002\u8981\u6c42css\u6587\u4ef6\u540e\u7f00\u540d\u662f<strong>.module.css<\/strong>\uff0c\u518d\u63d0\u524d\u914d\u5408\u597d\u6309\u9700\u52a0\u8f7d\uff08babel-plugin-import\uff09\uff0c\u5c31\u53ef\u4ee5\u5b8c\u7f8e\u4f7f\u7528\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import detailedStyles from'..\/styles\/pages\/detailed.module.css'<br> &nbsp; &lt;div className={styles.listTitle}&gt;{props.title}&lt;\/div&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">dangerouslySetInnerHTML<\/h2>\n\n\n\n<p>\u5728\u535a\u5ba2\u4e2d\uff0c\u9700\u8981\u52a8\u6001\u548c\u6b63\u786e\u5c55\u793amarkdown\u683c\u5f0f\u7684\u6587\u7ae0\u3002\u4f46\u662f\u6211\u4eec\u628a\u5199\u597d\u7684\u7684\u6587\u7ae0\u6570\u636e\u653e\u5728innerHtml\u91cc\u6e32\u67d3\uff0c\u53d1\u73b0\u5b83\u628ahtml\u6807\u7b7e\u6253\u51fa\u6765\u4e86\u3002\u89e3\u51b3\u529e\u6cd5\u662f\u52a0dangerouslySetInnerHTML\u8fd9\u4e2a\u5c5e\u6027\uff0c\u5c31\u4f1a\u6b63\u786e\u6e32\u67d3\u51fa\u6765\uff0c\u5177\u4f53\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &lt;div &nbsp;dangerouslySetInnerHTML={{ __html: html }} &gt;&lt;\/div&gt;<br> \/\/ &lt;div innerHtml={ html } &gt;&lt;\/div&gt; \u00d7<\/pre>\n\n\n\n<p>\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u8fd9\u662f\u63d0\u9192react\u5f00\u53d1\u8005\u8981\u5c3d\u91cf\u907f\u514d <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cross-site_scripting\">cross-site scripting (XSS)<\/a> \u653b\u51fb\uff0c\u60f3\u4e00\u60f3\uff0c\u4e5f\u8bb8\u574f\u4eba\u4f1a\u901a\u8fc7HTTP\u52ab\u6301\u4f60\u7684\u6587\u7ae0\u6587\u4ef6\uff0c\u88ab\u4fee\u6539\u6587\u4ef6\u6765\u5b9e\u73b0\u6076\u610f\u4ee3\u7801\u7684\u6ce8\u5165\u3002\u6240\u4ee5react\u5b98\u65b9\u63a8\u8350\u4f7f\u7528<code>dangerouslySetInnerHTML<\/code> \u548c\u5411\u5176\u4f20\u9012\u5305\u542b key \u4e3a <code>__html<\/code> \u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u4e0d\u7ba1\u600e\u6837\uff0c\u5b83\u5728\u6570\u636e\u4e2d\u8fd8\u662f\u5448\u73b0\u6240\u6709HTML\uff0c\u4e0d\u7ba1\u4ee3\u7801\u662f\u826f\u6027\u7684\u8fd8\u662f\u5371\u9669\u7684\u3002\u6587\u6863\u4e5f\u6ca1\u7ed9\u51fa\u89e3\u51b3xss\u7684\u529e\u6cd5\uff08\u8fd8\u662f\u6211\u6ca1\u770b\u89c1\uff09\uff0c\u53ea\u662f\u8d77\u540d\uff1a\u8fd9\u5f88\u5371\u9669\uff01\u5982\u679c\u6211\u4eec\u4f7f\u7528dangerouslySetinnerHTML\u8fdb\u884c\u4ee3\u7801\u5b89\u5168\u5ba1\u67e5\u5e76\u5c06\u5176\u653e\u5230\u9875\u9762\u4e2d\uff0c\u5c31\u4e0d\u4f1a\u5f15\u8d77\u8de8\u7ad9\u70b9\u811a\u672c\u95ee\u9898\u3002\u800c<a href=\"https:\/\/pragmaticwebsecurity.com\/articles\/spasecurity\/react-xss-part2.html\">DOMPurify<\/a>\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u3001\u5b89\u5168\u7684HTML\u5b89\u5168\u5ba1\u67e5\u5de5\u5177\uff0c\u7531\u4e00\u4e2a\u5fb7\u56fd\u7684XSS\u4e13\u5bb6\u56e2\u961f\u6784\u5efa\u3002\u5b83\u53ef\u4ee5\u68c0\u6d4b\u51fa\u548c\u53bb\u9664\u5371\u9669\u4ee3\u7801\uff0c\u53c8\u4e0d\u5f71\u54cd\u8282\u70b9\u7684\u6e32\u67d3\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8981\u4e0b\u8f7dnpm\u5305\u624d\u53ef\u4ee5\u4f7f\u7528\u3002\u51c0\u5316html\u4ee3\u7801\uff0c\u907f\u514dxss\u5e94\u8be5\u4f1a\u6709\u5f88\u591a\u5f88\u591a\u529e\u6cd5\uff0c\u8fd9\u4e2a\u9700\u8981\u4e0d\u65ad\u5b66\u4e60\u548c\u4e86\u89e3~<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;div dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(html)}}&gt;&lt;\/div&gt;);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u5e93\u8fde\u63a5<\/h2>\n\n\n\n<p>\u8fde\u63a5\u6570\u636e\u5e93\u548c\u7f16\u5199\u63a5\u53e3\u4f7f\u7528\u4e86\u57fa\u4e8ekoa\u6846\u67b6\u7684egg.js\u3002\u5728egg.js\u4e2d\u4f7f\u7528mysql\u6570\u636e\u5e93\uff0c\u9700\u8981\u5b89\u88c5<strong><code>egg-mysql<\/code>\u6a21\u5757<\/strong>\u5e76\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\u5c31\u53ef\u4ee5\u4e86\u3002\u6211\u7684\u6570\u636e\u5e93\u90e8\u7f72\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/\u9879\u76ee\u535a\u5ba21.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/\u9879\u76ee\u535a\u5ba21.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c06UTC\u683c\u5f0f\u8f6c\u4e3a\u672c\u5730\u65f6\u95f4<\/h3>\n\n\n\n<p>\u7f16\u5199\u63a5\u53e3\u3001\u8bf7\u6c42\u6570\u636e\u65b9\u6cd5\u53ea\u8981\u6b63\u786e\u7f16\u5199\u90fd\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u662f\u6211\u53d1\u73b0\u6211\u7684\u65e5\u671f\u6570\u636e\u7684\u4f20\u9012\u5f88\u6709\u95ee\u9898\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205111911687.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205111911687.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" style=\"width:215px;height:34px\"\/><\/div><\/figure>\n\n\n\n<p>\u9996\u5148\uff0c\u6570\u636e\u5e93\u7684addTime\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u662fdate\u7c7b\u578b\uff0c\u5e76\u4e14\u6dfb\u52a0\u7684\u8bb0\u5f55\u503c\u662f\uff1a2021-01-20\uff0c\u76f4\u63a5\u62ff\u5230\u9875\u9762\u6765\u663e\u793a\u5374\u53d8\u6210\u4e86\u8fd9\u4e2a\u6837\u5b50\uff0c\u572820\u53f7\u51cc\u6668\u96f6\u70b9\u7684\u57fa\u7840\u4e0a\u51cf\u5c11\u4e868\u4e2a\u5c0f\u65f6\uff08\u5f80\u897f8\u8dd1\u4e2a\u65f6\u533a\u5230\u4f26\u6566\u53bb\u4e86\uff09\uff0c\u6240\u4ee5\u9875\u9762\u663e\u793a\u7684\u662f\u683c\u6797\u5c3c\u6cbb\u6807\u51c6\u65f6\u95f4\uff08Greenwich Mean Time\uff0cGMT\uff09\uff0c\u4f46\u73b0\u5728\u7684\u6807\u51c6\u65f6\u95f4\u66f4\u591a\u7684\u662f\u8bf4\uff1a\u534f\u8c03\u4e16\u754c\u65f6\uff08UTC\uff09\u2014\u2014\u7531\u539f\u5b50\u949f\u63d0\u4f9b\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5 \u2460\u7528JavaScript\u7684Date\u7684\u65b9\u6cd5\uff0c\u5c06UTC\u683c\u5f0f\u8f6c\u5316\u4e3a\u6211\u4eec\u672c\u5730\u65f6\u95f4\u7684\u64cd\u4f5c\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &lt;span &gt;&lt;EditOutlined \/&gt; {new Date(item.addTime).toDateString()}&lt;\/span&gt;<br>\/\/\u6216\u8005<br> {new Date(item.addTime).toLocaleDateString()}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205145919921.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205145919921.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205150505406.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205150505406.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<p>\u6216\u8005\uff0c\u2461\u4ecesql\u8bed\u53e5\u91cc\u505a\u66f4\u6539\uff1aDATE_FORMAT(article.addTime,&#8217;%Y-%c-%d&#8217;)\uff0c\u76f4\u63a5\u8fd4\u56de\u6b63\u786e\u683c\u5f0f\u7684\u65f6\u95f4\u6570\u636e\u7ed9\u524d\u7aef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'SELECT article.id as id,' +<br>        ........<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"DATE_FORMAT(article.addTime,'%Y-%c-%d')  as addTime,\" +<br> &nbsp; &nbsp; &nbsp;  ........<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'FROM article LEFT JOIN type ON article.type_id = type.Id ' <\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205155516138.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205155516138.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u8de8\u57df\u95ee\u9898<\/h2>\n\n\n\n<p>\u7b2c\u4e00\u6b21\uff08\u81ea\u5df1\u7ed9\u81ea\u5df1\uff09\u62c5\u4efb\u540e\u7aef\uff0c\u9047\u5230\u8de8\u57df\u95ee\u9898\u662f\u53c8\u6fc0\u52a8\u53c8\u5fc3\u614c\u7684\u3002<\/p>\n\n\n\n<p>\u975e\u540c\u6e90\u8bf7\u6c42\uff0c\u5747\u4e3a\u8de8\u57df\u3002<em>\u540d\u8bcd\u89e3\u91ca\uff1a\u540c\u6e90 \u2014\u2014 \u5982\u679c\u4e24\u4e2a\u9875\u9762\u62e5\u6709\u76f8\u540c\u7684\u534f\u8bae\uff08protocol\uff09\uff0c\u7aef\u53e3\uff08port\uff09\u548c\u4e3b\u673a\uff08host\uff09\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u9875\u9762\u5c31\u5c5e\u4e8e\u540c\u4e00\u4e2a\u6e90\uff08origin\uff09\u3002<\/em>\u6211\u7684\u8de8\u57df\u95ee\u9898\u662f\u7531\u4e8e\u7aef\u53e3\u95ee\u9898\u4e0d\u540c\u800c\u9020\u6210\u7684\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\u8de8\u57df\u95ee\u9898\u4e3b\u8981\u91c7\u53d6\u7684\u65b9\u6cd5\u6709\uff1aJSONP\u3001CORS\u3001postMessage\u3001proxy\u4ee3\u7406\u7b49\u7b49\u3002\u8fd9\u91cc\u901a\u8fc7\u4f7f\u7528egg-cors\u63d2\u4ef6\u6765\u5b8c\u6210CORS\u65b9\u6cd5\u7684\u8de8\u57df\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/eggjs\/egg-cors\"><code>egg-cors<\/code>\u6a21\u5757<\/a>\u662f\u4e13\u95e8\u7528\u6765\u89e3\u51b3<code>egg.js<\/code>\u8de8\u57df\u95ee\u9898\u7684\uff0c\u53ea\u8981\u7b80\u5355\u7684\u914d\u7f6e\u5c31\u53ef\u4ee5\u5b8c\u6210\u8de8\u57df\u7684\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> config.security = {<br> &nbsp;  \u3000\u3000\u3000\u3000csrf: {<br> &nbsp;  \u3000\u3000\u3000\u3000\u3000\u3000enable: false<br> &nbsp;  \u3000\u3000\u3000\u3000},<br> &nbsp;  \u3000\u3000\u3000\u3000domainWhiteList: [ '*' ]<br> &nbsp;  \u3000\u3000};<br>    config.cors = {<br> &nbsp; &nbsp; &nbsp; &nbsp;origin: 'http:\/\/localhost:3000', \/\/\u670d\u52a1\u5668\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp;credentials: true,\/\/\u5141\u8bb8cookie\u8de8\u57df<br> &nbsp; &nbsp; &nbsp; &nbsp;withCredentials: true, &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS'<br> &nbsp;  };<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">react-router-dom(5.x)\u4f7f\u7528<\/h2>\n\n\n\n<p>\u7f16\u5199\u540e\u53f0\u7ba1\u7406\u5e94\u7528\u9875\u9762\uff0c\u662f\u76f4\u63a5\u4f7f\u7528\u4e86create-react-app\u53bb\u6784\u5efa\u7684\uff0c\u6240\u4ee5\u8def\u7531\u90e8\u5206\u5f53\u7136\u7528\u8d77\u4e86react-router-dom\u3002\u4e0d\u8fc7\u4f7f\u7528\u7684\u8fc7\u7a0b\uff0c\u8fd8\u662f\u9047\u5230\u5f88\u591a\u95ee\u9898\u7684\uff0c\u6bd4\u5982\u8bf4\u4f20\u9012url\u7684\u53c2\u6570\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u770b\u6587\u6863\u554a\uff01\uff01\uff01<\/p>\n\n\n\n<p>\u5728\u5728React Router v4\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4eceprops\u4e2d\u83b7\u5f97history\u5bf9\u8c61\u3001url\u53c2\u6570\u2026\u2026\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let tmpId = props.match.params.id<br>\u200b<br>&lt;button type=\"button\" onClick={() =&gt; props.history.goBack()}&gt;<\/pre>\n\n\n\n<p>\u7136\u800c\u4ece5.1\u7248\u5f00\u59cb\uff0cReact Router\u5c06\u5305\u62ec\u56db\u4e2a\u6709\u7528\u7684\u94a9\u5b50\uff0c\u8fd9\u4e5f\u662f\u6b64\u6b21\u9879\u76ee\u5b9e\u8df5\u4e2d\u5b66\u4e60\u5230\u7684\u3002\u4f60\u53ef\u4ee5\u7528\u5b83\u4eec\u6765\u7f16\u5199\u66f4\u5e72\u51c0\u6574\u6d01\u7684\u4ee3\u7801\uff0c\u8ba9\u4eba\u8d4f\u5fc3\u60a6\u76ee~<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">UseParams<\/h3>\n\n\n\n<p>useParams\u94a9\u5b50\u5c06\u4ece\u4f60\u7684\u5e94\u7528URL\u8fd4\u56de\u4e00\u4e2a\u952e\/\u503c\u5bf9\u5bf9\u8c61\u3002\u5728\u4e00\u4e2a\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u901a\u5e38\u4f1a\u6709\u8bb8\u591a\u52a8\u6001\u7684\u5bfc\u822a\u94fe\u63a5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;&lt;Route path=\"\/adminIndex\/addArticle\/:id\"&gt;<br> &nbsp;\u2026\u2026<br> &nbsp;\/\/addArticle.js<br> &nbsp; let {id} &nbsp;= useParams(); <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">UseLocation<\/h3>\n\n\n\n<p>\u53ef\u4ee5\u8bbf\u95eelocation\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let location = useLocation();<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205210204016.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/qqiukulele.cn\/wp-content\/uploads\/2021\/02\/image-20210205210204016.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\"\/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">useHistory<\/h3>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528useHistory\u94a9\u5b50\uff0c\u4f60\u4e0d\u9700\u8981Route\u7ec4\u4ef6\u6765\u4f20\u9012\u5b83\u5c31\u83b7\u5f97history\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function Users() {<br> &nbsp;let history = useHistory();<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp; &nbsp;&lt;button type=\"button\" onClick={() =&gt; history.goBack()}&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;Go back<br> &nbsp; &nbsp; &nbsp;&lt;\/button&gt;<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  );<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">useRouteMatch<\/h3>\n\n\n\n<p>\u7701\u53bb\u4e86\u7f16\u5199&lt;Route&gt;\u7ec4\u4ef6\u5c31\u53ef\u4ee5\u8ba9\u7ec4\u4ef6\u53bb\u8bbf\u95ee\u5339\u914d\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ before<br>import { Route } from 'react-router-dom'<br>\u200b<br>function App() {<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp;  {\/* ... *\/}<br> &nbsp; &nbsp; &nbsp;&lt;Route<br> &nbsp; &nbsp; &nbsp; &nbsp;path=\"\/BLOG\/:slug\/\"<br> &nbsp; &nbsp; &nbsp; &nbsp;strict<br> &nbsp; &nbsp; &nbsp; &nbsp;sensitive<br> &nbsp; &nbsp; &nbsp; &nbsp;render={({ match }) =&gt; {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return match ? &lt;BlogPost match={match} \/&gt; : &lt;NotFound \/&gt;<br> &nbsp; &nbsp; &nbsp;  }}<br> &nbsp; &nbsp; &nbsp;\/&gt;<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  )<br>}<br>\u200b<br>\/\/ after<br>import { useRouteMatch } from 'react-router-dom'<br>\u200b<br>function App() {<br> &nbsp;let match = useRouteMatch({<br> &nbsp; &nbsp;path: '\/BLOG\/:slug\/',<br> &nbsp; &nbsp;strict: true,<br> &nbsp; &nbsp;sensitive: true<br>  })<br>\u200b<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp;  {\/* ... *\/}<br> &nbsp; &nbsp;  {match ? &lt;BlogPost match={match} \/&gt; : &lt;NotFound \/&gt;}<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  )<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u5176\u5b9e\u5199\u8fd9\u4e2a\u5c0f\u9879\u76ee\u7684\u8fc7\u7a0b\u4f1a\u9047\u5230\u8bb8\u8bb8\u591a\u591a\u7684errors\uff0c\u4f46\u662f\u7ecf\u8fc7\u53cd\u590d\u5730\u5ba1\u67e5\u4ee3\u7801\uff0c\u603b\u5f52\u4e00\u4e00\u89e3\u51b3\u3002\u6700\u5927\u7684\u6536\u83b7\u662f\uff0c\u5c1d\u8bd5\u548c\u5b66\u4f1a\u4f7f\u7528\u4e86\u5f88\u591a\u6709\u7528\u65b9\u4fbf\u7684\u5e93\u3001\u66f4\u7406\u89e3\u4e86\u6574\u4e2a\u524d\u53f0\u540e\u53f0\u600e\u4e48\u4ea4\u4e92\u7684\u3001\u4e0d\u65ad\u770b\u6587\u6863\u548c\u5b66\u4e60\u5de9\u56fa\u77e5\u8bc6\u7b49\u7b49\u2026\u2026\u6700\u5927\u7684\u611f\u53d7\u662f\uff1a\u524d\u7aef\u6280\u672f\u8fed\u4ee3\u592a\u5feb\u4e86\uff0c1\u5e74\u524d\u7684\u7528\u6cd5\u53ef\u80fd\u5c31\u8fc7\u65f6\u4e86\uff0c\u7a81\u7136\u5c31\u6709\u70b9\u75b2\u60eb\u2026\u2026\u8fd8\u6709\u597d\u591a\u4e1c\u897f\u6ca1\u5b66\u5462\u545c\u545c\u545c\u545c\u545c\u545c\u545c<\/p>\n\n\n\n<p>\u53c2\u8003\u548c\u5b66\u4e60\u94fe\u63a5\uff1a<\/p>\n\n\n\n<p>\u2460 <a href=\"https:\/\/juejin.cn\/post\/6844903496521613320\">\u8de8\u57df<\/a><\/p>\n\n\n\n<p>\u2461 <a href=\"https:\/\/reacttraining.com\/blog\/react-router-v5-1\/\">React Router v5.1<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u4e3b\u6d41\u4e0a\u7ebf\uff1a\u90a3\u4e9b\u72af\u4e0b\u7684\u9519\u8bef\u3001\u3001\u535f\u613f\u56de\u9996&#038;&#038;\u5e78\u798f\u7684\u6469\u5929\u8f6e\u4e0d\u8f6c\u4e86<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[9],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-21","tag-react"],"_links":{"self":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":5,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/487"}],"wp:attachment":[{"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qqiukulele.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}