You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
8.9 KiB
8.9 KiB
order | title |
---|---|
2 | 快速上手 |
Ant Design React 致力于提供给程序员愉悦的开发体验。
在开始之前,推荐先学习 React 和 ES2015,并正确安装和配置了 Node.js v8 或以上。官方指南假设你已了解关于 HTML、CSS 和 JavaScript 的中级知识,并且已经完全掌握了 React 全家桶的正确开发方式。如果你刚开始学习前端或者 React,将 UI 框架作为你的第一步可能不是最好的主意。
第一个例子
这是一个最简单的 Ant Design 组件的在线 codesandbox 演示。
<h3 id="user-content-1-创建一个-codesandbox">1. 创建一个 codesandbox</h3> <p>访问 http://u.ant.design/codesandbox-repro 创建一个 codesandbox 的在线示例,别忘了保存以创建一个新的实例。</p> <h3 id="user-content-2-使用组件">2. 使用组件</h3> <p>直接用下面的代码替换 <code>index.js</code> 的内容,用 React 的方式直接使用 antd 组件。</p> <pre><code class="chroma language-jsx"><span class="kr">import</span> <span class="nx">React</span> <span class="nx">from</span> <span class="s1">'react'</span><span class="p">;</span> <span class="kr">import</span> <span class="nx">ReactDOM</span> <span class="nx">from</span> <span class="s1">'react-dom'</span><span class="p">;</span> <span class="kr">import</span> <span class="p">{</span> <span class="nx">ConfigProvider</span><span class="p">,</span> <span class="nx">DatePicker</span><span class="p">,</span> <span class="nx">message</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">'antd'</span><span class="p">;</span> <span class="c1">// 由于 antd 组件的默认文案是英文,所以需要修改为中文 </span><span class="c1"></span><span class="kr">import</span> <span class="nx">zhCN</span> <span class="nx">from</span> <span class="s1">'antd/es/locale/zh_CN'</span><span class="p">;</span> <span class="kr">import</span> <span class="nx">moment</span> <span class="nx">from</span> <span class="s1">'moment'</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">'moment/locale/zh-cn'</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">'antd/dist/antd.css'</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">'./index.css'</span><span class="p">;</span> <span class="nx">moment</span><span class="p">.</span><span class="nx">locale</span><span class="p">(</span><span class="s1">'zh-cn'</span><span class="p">);</span> <span class="kr">class</span> <span class="nx">App</span> <span class="kr">extends</span> <span class="nx">React</span><span class="p">.</span><span class="nx">Component</span> <span class="p">{</span> <span class="nx">state</span> <span class="o">=</span> <span class="p">{</span> <span class="nx">date</span><span class="o">:</span> <span class="kc">null</span><span class="p">,</span> <span class="p">};</span> <span class="nx">handleChange</span> <span class="o">=</span> <span class="nx">date</span> <span class="p">=></span> <span class="p">{</span> <span class="nx">message</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="sb">`您选择的日期是: </span><span class="si">${</span><span class="nx">date</span> <span class="o">?</span> <span class="nx">date</span><span class="p">.</span><span class="nx">format</span><span class="p">(</span><span class="s1">'YYYY-MM-DD'</span><span class="p">)</span> <span class="o">:</span> <span class="s1">'未选择'</span><span class="si">}</span><span class="sb">`</span><span class="p">);</span> <span class="k">this</span><span class="p">.</span><span class="nx">setState</span><span class="p">({</span> <span class="nx">date</span> <span class="p">});</span> <span class="p">};</span> <span class="nx">render</span><span class="p">()</span> <span class="p">{</span> <span class="kr">const</span> <span class="p">{</span> <span class="nx">date</span> <span class="p">}</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">state</span><span class="p">;</span> <span class="k">return</span> <span class="p">(</span> <span class="p"><</span><span class="nt">ConfigProvider</span> <span class="na">locale</span><span class="o">=</span><span class="p">{</span><span class="nx">zhCN</span><span class="p">}></span> <span class="p"><</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="p">{{</span> <span class="nx">width</span><span class="o">:</span> <span class="mi">400</span><span class="p">,</span> <span class="nx">margin</span><span class="o">:</span> <span class="s1">'100px auto'</span> <span class="p">}}></span> <span class="p"><</span><span class="nt">DatePicker</span> <span class="na">onChange</span><span class="o">=</span><span class="p">{</span><span class="k">this</span><span class="p">.</span><span class="nx">handleChange</span><span class="p">}</span> <span class="p">/></span> <span class="p"><</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="p">{{</span> <span class="nx">marginTop</span><span class="o">:</span> <span class="mi">20</span> <span class="p">}}></span> <span class="nx">当前日期</span><span class="err">:</span><span class="p">{</span><span class="nx">date</span> <span class="o">?</span> <span class="nx">date</span><span class="p">.</span><span class="nx">format</span><span class="p">(</span><span class="s1">'YYYY-MM-DD'</span><span class="p">)</span> <span class="o">:</span> <span class="s1">'未选择'</span><span class="p">}</span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> <span class="p"></</span><span class="nt">ConfigProvider</span><span class="p">></span> <span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">ReactDOM</span><span class="p">.</span><span class="nx">render</span><span class="p">(<</span><span class="nt">App</span> <span class="p">/>,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'root'</span><span class="p">));</span> </code></pre><h3 id="user-content-3-探索更多组件用法">3. 探索更多组件用法</h3> <p>你可以在组件页面的左侧菜单查看组件列表,比如 <a href="/components/alert">Alert</a> 组件,组件文档中提供了各类演示,最下方有组件 API 文档可以查阅。在代码演示部分找到第一个例子,点击右下角的图标展开代码。</p> <p>然后依照演示代码的写法,在之前的 codesandbox 里修改 <code>index.js</code>,首先在 <code>import</code> 内引入 Alert 组件:</p> <pre><code class="chroma language-diff"><span class="gd">- import { ConfigProvider, DatePicker, message } from 'antd'; </span><span class="gd"></span><span class="gi">+ import { ConfigProvider, DatePicker, message, Alert } from 'antd'; </span></code></pre><p>然后在 <code>render</code> 内添加相应的 jsx 代码:</p> <pre><code class="chroma language-diff"> <DatePicker onChange={value => this.handleChange(value)} /> <div style={{ marginTop: 20 }}> <span class="gd">- 当前日期:{date ? date.format('YYYY-MM-DD') : '未选择'} </span><span class="gd"></span><span class="gi">+ <Alert message={`当前日期:${date ? date.format('YYYY-MM-DD') : '未选择'}`} type="success" /> </span><span class="gi"></span> </div> </code></pre><p>在右侧预览区就可以看到如图的效果。</p> <p><img width="420" src="https://gw.alipayobjects.com/zos/antfincdn/Up3%24VYhN0S/134614ee-7440-46f1-a797-fa6f6b3e300f.png" alt="codesandbox screenshot" /></p> <p>好的,现在你已经会使用基本的 antd 组件了,你可以在这个例子中继续探索其他组件的用法。如果你遇到组件的 bug,也推荐建一个可重现的 codesandbox 来报告 bug。</p> <h3 id="user-content-4-下一步">4. 下一步</h3> <p>实际项目开发中,你会需要构建、调试、代理、打包部署等一系列工程化的需求。您可以阅读后面的文档或者使用以下脚手架和范例:</p> <ul> <li><a href="http://pro.ant.design/">Ant Design Pro</a></li> <li><a href="https://github.com/zuiidea/antd-admin">antd-admin</a></li> <li><a href="https://github.com/d2-projects/d2-admin">d2-admin</a></li> <li>更多脚手架可以查看 <a href="http://scaffold.ant.design/">脚手架市场</a></li> </ul> <h2 id="user-content-兼容性">兼容性</h2> <p>Ant Design React 支持所有的现代浏览器和 IE11+。</p> <table> <thead> <tr> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" /></a></br>IE / Edge</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" /></a></br>Firefox</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" /></a></br>Chrome</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" /></a></br>Safari</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png" alt="Opera" width="24px" height="24px" /></a></br>Opera</th> <th><a href="http://godban.github.io/browsers-support-badges/"><img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/electron/electron_48x48.png" alt="Electron" width="24px" height="24px" /></a></br>Electron</th> </tr> </thead> <tbody> <tr> <td>IE11, Edge</td> <td>last 2 versions</td> <td>last 2 versions</td> <td>last 2 versions</td> <td>last 2 versions</td> <td>last 2 versions</td> </tr> </tbody> </table> <p>对于 IE 系列浏览器,需要提供相应的 Polyfill 支持,建议使用 <a href="https://babeljs.io/docs/en/babel-preset-env">babel-preset-env</a> 来解决浏览器兼容问题。如果你在使用 <a href="http://umijs.org/">umi</a>,可以直接使用 <a href="https://umijs.org/zh/config/#targets">targets</a> 配置。</p> <p>Ant Design 3.0 对 React 15/16 两个版本提供支持,但是我们强烈建议你升级到 React 16,以便获得更好的性能和遇到更少的问题。</p> <h4 id="user-content-ie-note">IE note</h4> <blockquote> <p><code>antd@2.0</code> 之后将不再支持 IE8,<code>antd@4.0</code> 之后将不再支持 IE9/10。</p> </blockquote> <h2 id="user-content-自行构建">自行构建</h2> <p>如果想自己维护工作流,我们推荐使用 <a href="http://webpack.github.io/">webpack</a> 进行构建和调试。理论上你可以利用 React 生态圈中的 <a href="https://github.com/enaqx/awesome-react#boilerplates">各种脚手架</a> 进行开发,如果遇到问题可参考我们所使用的 <a href="https://github.com/ant-tool/atool-build/blob/master/src/getWebpackCommonConfig.js">webpack 配置</a> 进行 <a href="http://ant-tool.github.io/webpack-config.html">定制</a>。</p> <p>如果你使用 <a href="https://parceljs.org">parcel</a>,这里也有 <a href="https://github.com/ant-design/parcel-antd">一个例子</a> 可以参考。</p> <p>目前社区也有很多基于 antd 定制的 <a href="http://scaffold.ant.design/">脚手架</a>,欢迎进行试用和贡献。</p> <h2 id="user-content-按需加载">按需加载</h2> <p>如果你在开发环境的控制台看到下面的提示,那么你可能使用了 <code>import { Button } from 'antd';</code> 的写法引入了 antd 下所有的模块,这会影响应用的网络性能。</p> <pre><code class="chroma language-text">You are using a whole package of antd, please use https://www.npmjs.com/package/babel-plugin-import to reduce app bundle size. </code></pre><blockquote> <p><a href="https://zos.alipayobjects.com/rmsportal/GHIRszVcmjccgZRakJDQ.png"><img src="https://zos.alipayobjects.com/rmsportal/GHIRszVcmjccgZRakJDQ.png" alt="控制台警告"></a></p> </blockquote> <p>可以通过以下的写法来按需加载组件。</p> <pre><code class="chroma language-jsx"><span class="kr">import</span> <span class="nx">Button</span> <span class="nx">from</span> <span class="s1">'antd/es/button'</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">'antd/es/button/style'</span><span class="p">;</span> <span class="c1">// 或者 antd/es/button/style/css 加载 css 文件 </span></code></pre><blockquote> <p>注意:antd 默认支持基于 ES module 的 tree shaking,对于 js 部分,直接引入 <code>import { Button } from 'antd'</code> 也会有按需加载的效果。</p> </blockquote> <p>如果你使用了 babel,那么可以使用 <a href="https://github.com/ant-design/babel-plugin-import">babel-plugin-import</a> 来进行按需加载,加入这个插件后。你可以仍然这么写:</p> <pre><code class="chroma language-jsx"><span class="kr">import</span> <span class="p">{</span> <span class="nx">Button</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">'antd'</span><span class="p">;</span> </code></pre><p>插件会帮你转换成 <code>antd/es/xxx</code> 的写法。另外此插件配合 <a href="https://github.com/ant-design/babel-plugin-import#usage">style</a> 属性可以做到模块样式的按需自动加载。</p> <blockquote> <p>注意,babel-plugin-import 的 <code>style</code> 属性除了引入对应组件的样式,也会引入一些必要的全局样式。如果你不需要它们,建议不要使用此属性。你可以 <code>import 'antd/dist/antd.css'</code> 手动引入,并覆盖全局样式。</p> </blockquote> <h2 id="user-content-使用-day-js-替换-momentjs-优化打包大小">使用 Day.js 替换 momentjs 优化打包大小</h2> <p>你可以使用 <a href="https://github.com/ant-design/antd-dayjs-webpack-plugin">antd-dayjs-webpack-plugin</a> 插件用 Day.js 替换 momentjs 来大幅减小打包大小。这需要更新 webpack 的配置文件如下:</p> <pre><code class="chroma language-js"><span class="c1">// webpack-config.js </span><span class="c1"></span><span class="kr">import</span> <span class="nx">AntdDayjsWebpackPlugin</span> <span class="nx">from</span> <span class="s1">'antd-dayjs-webpack-plugin'</span><span class="p">;</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span> <span class="c1">// ... </span><span class="c1"></span> <span class="nx">plugins</span><span class="o">:</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AntdDayjsWebpackPlugin</span><span class="p">()],</span> <span class="p">};</span> </code></pre><h2 id="user-content-配置主题和字体">配置主题和字体</h2> <ul> <li><a href="/docs/react/customize-theme">改变主题</a></li> <li><a href="https://github.com/ant-design/antd-init/tree/master/examples/local-iconfont">使用本地字体</a></li> </ul>