{"id":503,"date":"2022-11-05T11:31:53","date_gmt":"2022-11-05T03:31:53","guid":{"rendered":"http:\/\/gjweb.top\/?p=503"},"modified":"2022-11-05T11:31:54","modified_gmt":"2022-11-05T03:31:54","slug":"umi%e9%a1%b9%e7%9b%ae%e9%97%ae%e9%a2%98%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=503","title":{"rendered":"umi\u9879\u76ee\u95ee\u9898\u603b\u7ed3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"\u8d77\u6b65\">\u8d77\u6b65<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn create @umijs\/umi-app<br>\/\/ or<br>npx @umijs\/create-umi-app<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"umi-\u7ea6\u5b9a\u5f0f\u8def\u7531\">umi \u7ea6\u5b9a\u5f0f\u8def\u7531<\/h2>\n\n\n\n<p>umi \u63d0\u4f9b\u4e86\u7ea6\u5b9a\u5f0f\u7406\u7531\u5199\u6cd5\u5373\u521b\u5efa\u6587\u4ef6\u81ea\u52a8\u8f6c\u5316\u4e3a\u8def\u7531<\/p>\n\n\n\n<p>\u5728 src \/ pages \u4e2d\u521b\u5efa\u6587\u4ef6 umi \u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u9875\u9762\u7684\u8def\u7531, \u9ed8\u8ba4\u4e3a index.tsx \u4e3a\u9996; \u9ed8\u8ba4 404.tsx \u4e3a\u62a5\u9519\u9875\u9762<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u8def\u7531\u5d4c\u5957\">\u8def\u7531\u5d4c\u5957<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/gjweb.top\/wp-content\/uploads\/2022\/11\/1667572684449.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"213\" height=\"313\" data-original=\"http:\/\/gjweb.top\/wp-content\/uploads\/2022\/11\/1667572684449.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-504\"  sizes=\"auto, (max-width: 213px) 100vw, 213px\" \/><\/div><\/figure>\n\n\n\n<p>\u5982\u4e0a\u56fe\u6240\u793a\u5728 pages \u6587\u4ef6\u5939\u4e0a\u9762 \u521b\u5efa _layout.tsx \u7528\u4e8e\u5e03\u5c40\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>const Home:React.FC&lt;any&gt; = (props)=&gt;{<br> &nbsp;return  (<br> &nbsp; &nbsp;&lt;div&gt;<br> &nbsp; &nbsp;  {props.children} \/\/ \u8fd9\u91cc\u7684 children \u6307\u7684\u662f\u9875\u9762\u7684\u5b50\u8def\u7531<br> &nbsp; &nbsp;&lt;\/div&gt;<br>  )<br>}<br>export default Home<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa index.tsx \u4f5c\u4e3a \/home \u8def\u7531\u7684\u9ed8\u8ba4\u5165\u53e3,\u5176\u4ed6\u5b50\u8def\u7531\u53ef\u4ee5\u4efb\u52a1\u547d\u540d, \u5728\u6d4f\u89c8\u5668\u8bbf\u95ee localhost:8000\/home\/xxx<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"umi-\u7f16\u7a0b\u5f0f\u5bfc\u822a\">umi \u7f16\u7a0b\u5f0f\u5bfc\u822a<\/h2>\n\n\n\n<p>\u9700\u8981\u5f15\u5165 umi \u5185\u7f6e\u7684 history \u5bf9\u8c61\u8fdb\u884c\u7f16\u7a0b\u5f0f\u5bfc\u822a\u7684\u8df3\u8f6c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import {history} from \"umi\"<br>const App = ()=&gt;{<br> &nbsp; &nbsp;const handelClick = ()=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;history.push('\/xxx')<br> &nbsp;  }<br> &nbsp; &nbsp;return (<br> &nbsp;      &lt;button onClick={handelClick}&gt;click&lt;\/button&gt;<br> &nbsp;  )<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"dva-\u72b6\u6001\u7ba1\u7406\">dva \u72b6\u6001\u7ba1\u7406<\/h2>\n\n\n\n<p>\u521b\u5efa models \/ user.js \u6587\u4ef6 \u6ce8\u610f <strong>\u6587\u4ef6\u540d\u5c31\u662f\u547d\u540d\u7a7a\u95f4<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default {<br> &nbsp;namespace: 'user',<br> &nbsp;state: {<br> &nbsp; &nbsp;userName: userInfo ? userInfo.username : ''<br>  },<br> &nbsp;reducers: {<br> &nbsp; &nbsp;userConfig(state, { payload }) {<br> &nbsp; &nbsp; &nbsp;return { ...state, ...payload };<br> &nbsp;  },<br>  },<br>};<\/code><\/pre>\n\n\n\n<p>\u5728\u7ec4\u4ef6\u4e2d\u76d1\u542c\u6216\u4fee\u6539 user \u4e2d\u7684\u72b6\u6001<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\"<br>import { connect, useDispatch } from 'dva';<br>const Component:React.FC&lt;any&gt; = (props)=&gt;{<br> &nbsp; &nbsp;const distPath = useDispatch()<br> &nbsp; &nbsp;handelClick(){<br> &nbsp; &nbsp; &nbsp; &nbsp;distPath({<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type:'user\/userConfig',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;payload:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;userName:\"admin\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp;  }<br> &nbsp; &nbsp;return (<br> &nbsp;      &lt;div&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;h2&gt;this is {props.userName}&lt;\/h2&gt;<br> &nbsp; &nbsp; &nbsp;      &lt;button onClick={()=&gt;handelClick()}&gt;\u4fee\u6539username&lt;\/button&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br> &nbsp;  )<br>}<br>export defalut connect((state:any)=&gt;state.user)(Component)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4f7f\u7528-styled-components\">\u4f7f\u7528 styled-components<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5b89\u88c5\">\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add styled-components -S<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4f7f\u7528\">\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import styled from \"styled-components\"<br>const Wrapper = styled.div`<br>    css<br>`<br>const Wrapper = styled(\u7ec4\u4ef6\u540d\u79f0)`<br>    css<br>`<br>const APP = ()=&gt;{<br> &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;Wrapper&gt;&lt;\/Wrapper&gt;<br> &nbsp;  )<br>}<br>export defalut APP<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"umi-\u53cd\u5411\u4ee3\u7406\">umi \u53cd\u5411\u4ee3\u7406<\/h2>\n\n\n\n<p>\u5728 .umirc.js \u6587\u4ef6\u4e2d\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>proxy:{<br> &nbsp; &nbsp;\"\/api\":{<br> &nbsp; &nbsp; &nbsp; &nbsp;target:\"http:\/\/www.baidu.com\",<br> &nbsp; &nbsp; &nbsp; &nbsp;pathRewrite:{\"^\/api\":\"\"},<br> &nbsp; &nbsp; &nbsp; &nbsp;changeOrigin:true<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"umi-\u4e2d\u4f7f\u7528\u963f\u91cc\u56fe\u6807-svg\">umi \u4e2d\u4f7f\u7528\u963f\u91cc\u56fe\u6807 SVG<\/h2>\n\n\n\n<p>\u5728\u641c\u4e86\u5168\u7f51\u7684\u5173\u4e8e\u5f15\u5165\u963f\u91cc\u56fe\u8868\u7684\u65b9\u5f0f\uff0c\u5927\u90e8\u5206\u90fd\u4f7f\u7528\u7684\u5b57\u4f53\u56fe\u6807\u3002 \u4f46\u662f\u89c9\u5f97\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u592a\u4f18\u96c5\u3002\u4e0b\u9762\u662f\u7528umi \u4e2d\u5f15\u5165\u963f\u91cc SVG \u7684\u7528\u4f8b<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5c06\u8981\u662f\u7528\u7684\u56fe\u6807\u4ece\u5b98\u7f51\u4e0b\u8f7dSVG\u5230\u672c\u5730<\/li><li>\u5728 umi \u9879\u76ee\u7684\u6839\u76ee\u5f55 \u521b\u5efa public \u6587\u4ef6\u5939 \u7528\u4e8e\u5b58\u653e\u9759\u6001\u8d44\u6e90\u6587\u4ef6<\/li><li>\u5728 .umirc.ts \u4e2d\u914d\u7f6e\u9875\u9762\u52a0\u8f7d js \u6587\u4ef6<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>headScripts: &#91;<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;src: '\/js\/iconfont.js', \/\/ \u8fd9\u91cc\u7684\u8def\u5f84\u4e0d\u9700\u8981\u5305\u542b public \u6587\u4ef6<br> &nbsp;  },<br> ],<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>\u5c06 icon \u5c01\u88c5\u6210\u7ec4\u4ef6<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react';<br>import styled from 'styled-components';<br>\u200b<br>const SVG = styled.svg`<br> &nbsp;width: 2rem;<br> &nbsp;height: 2rem;<br> &nbsp;overflow: hidden;<br> &nbsp;color: #fff;<br>`;<br>interface Props {<br> &nbsp;iconName: String;<br>  &#91;propsName: string]: any;<br>}<br>\u200b<br>const Icon: React.FC&lt;Props&gt; = (props) =&gt; {<br> &nbsp;return (<br> &nbsp; &nbsp;&lt;SVG aria-hidden=\"true\"&gt;<br> &nbsp; &nbsp; &nbsp;&lt;use xlinkHref={`#${props.iconName}`}&gt;&lt;\/use&gt;<br> &nbsp; &nbsp;&lt;\/SVG&gt;<br>  );<br>};<br>export default Icon;<br>\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"umi\u4fee\u6539\u6d4f\u89c8\u5668-title-\u548c-icon\">umi\u4fee\u6539\u6d4f\u89c8\u5668 title \u548c ICON<\/h2>\n\n\n\n<p>\u5728 .umirc.ts \u6587\u4ef6\u4e2d\u6dfb\u52a0\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>title:'\u9f9a\u7bad\u56fe\u5e8a',<br>links:&#91;<br> &nbsp;  {rel:'icon',href:\"\"}<br>],<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"umi-\u66f4\u6539\u8def\u7531\u6a21\u5f0f\">umi \u66f4\u6539\u8def\u7531\u6a21\u5f0f<\/h2>\n\n\n\n<p>\u5728 .umirc.ts \u6587\u4ef6\u4e2d\u6dfb\u52a0\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>history:{<br> &nbsp; &nbsp;type:'hash'<br>},<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8d77\u6b65 umi \u7ea6\u5b9a\u5f0f\u8def\u7531 umi \u63d0\u4f9b\u4e86\u7ea6\u5b9a\u5f0f\u7406\u7531\u5199\u6cd5\u5373\u521b\u5efa\u6587\u4ef6\u81ea\u52a8\u8f6c\u5316\u4e3a\u8def\u7531 \u5728 src \/ pages \u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-503","post","type-post","status-publish","format-standard","hentry","category-umi"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/503","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=503"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/503\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}