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.
5.0 KiB
5.0 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="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">'react'</span><span class="p">;</span> <span class="kr">import</span> <span class="p">{</span> <span class="nx">render</span> <span class="p">}</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">dayjs</span> <span class="nx">from</span> <span class="s1">'dayjs'</span><span class="p">;</span> <span class="kr">import</span> <span class="s1">'dayjs/locale/zh-cn'</span><span class="p">;</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="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">dayjs</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">const</span> <span class="nx">App</span> <span class="o">=</span> <span class="p">()</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="nx">setDate</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span> <span class="kr">const</span> <span class="nx">handleChange</span> <span class="o">=</span> <span class="nx">value</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">value</span> <span class="o">?</span> <span class="nx">value</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="nx">setDate</span><span class="p">(</span><span class="nx">value</span><span class="p">);</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="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">16</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="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: 16 }}> <span class="gd">- 当前日期:{date ? date.format('YYYY-MM-DD') : '未选择'} </span><span class="gd"></span><span class="gi">+ <Alert message="当前日期" description={date ? date.format('YYYY年MM月DD日') : '未选择'} /> </span><span class="gi"></span> </div> </code></pre><p>选择一个日期,在右侧预览区就可以看到如图的效果。</p> <p><img width="420" src="https://gw.alipayobjects.com/zos/antfincdn/ZosQjL9pqe/e6179c89-21a9-44c9-aea4-3cc04af7ef25.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><code>antd</code> 默认支持基于 ES modules 的 tree shaking,对于 js 部分,直接引入 <code>import { Button } from 'antd'</code> 就会有按需加载的效果。</p> <p>如果你在开发环境的控制台看到下面的提示,那么你可能还在使用 <code>webpack@1.x</code> 或者 tree shaking 失效,请升级或检查相关配置。</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><p><a href="https://zos.alipayobjects.com/rmsportal/GHIRszVcmjccgZRakJDQ.png"><img src="https://zos.alipayobjects.com/rmsportal/GHIRszVcmjccgZRakJDQ.png" alt="控制台警告"></a></p> <h2 id="user-content-自行构建">自行构建</h2> <p>如果想自己维护工作流,我们推荐使用 <a href="https://webpack.github.io/">webpack</a> 进行构建和调试,可以使用 React 生态圈中的 <a href="https://github.com/enaqx/awesome-react#react-tools">各种脚手架</a> 进行开发。</p> <p>目前社区也有很多基于 antd 定制的 <a href="http://scaffold.ant.design/">React 脚手架</a>,欢迎进行试用和贡献。</p>