1. 程式人生 > >React學習之旅----使用優美的框架Antd

React學習之旅----使用優美的框架Antd

create-react-app react05

cd react05 

npm install react-router-dom axios url antd -S

yarn start

啟動專案後,在編輯器中開啟修改 src/App.css,在檔案頂部引入 antd/dist/antd.css。全域性引入對之後的效能優化有些累贅。

@import '~antd/dist/antd.css'
.App {
  text-align: center;
}

在App.js中import { Button } from 'antd';  引入按鈕元件

在render渲染函式中

<Button type="primary">Primary</Button>
<Button>Default</Button>
<Button type="dashed">Dashed</Button>
<Button type="danger">Danger</Button>

之後,我們就能看到效果了

App.js

import React, { Component } from 'react';
// import logo from './logo.svg';
import './App.css';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import './assets/css/index.css';
import routes from './model/routes'
// import { Button, Layout, Menu, Icon } from 'antd';
import { Layout, Menu, Icon } from 'antd';
const { Header, Sider, Content } = Layout;
class App extends Component {
  state = {
    collapsed: false,
  };
  toggle = () => {
    this.setState({
      collapsed: !this.state.collapsed,
    });
  }
  render () {
    return (
      <Router>
        <div className="App">
          {/* <img src={logo} className="App-logo" alt="logo" width='100px' /> */}
          <Layout>
            <Sider
              trigger={null}
              collapsible
              collapsed={this.state.collapsed}
            >
              <div className="logo" />

              <Menu theme="dark" mode="inline" defaultSelectedKeys={['1']}>
                <Menu.Item key="1">
                  <Icon type="user" />
                  <span><Link to="/">首頁</Link></span>
                </Menu.Item>
                <Menu.Item key="2">
                  <Icon type="video-camera" />
                  <span><Link to="/user">使用者</Link></span>
                </Menu.Item>
                <Menu.Item key="3">
                  <Icon type="upload" />
                  <span><Link to="/shop">商戶</Link></span>
                </Menu.Item>
                <Menu.Item key="4">
                  <Icon type="upload" />
                  <span><Link to="/news">新聞</Link></span>
                </Menu.Item>
              </Menu>
            </Sider>
            <Layout>
              <Header style={{ background: '#fff', padding: 0 }}>
                <Icon
                  className="trigger"
                  type={this.state.collapsed ? 'menu-unfold' : 'menu-fold'}
                  onClick={this.toggle}
                />
              </Header>
              <Content style={{ margin: '24px 16px', padding: 24, background: '#fff', minHeight: 280 }}>
                {
                  routes.map((value, key) => {
                    if (value.exact) {
                      // 向子元件傳值
                      return <Route key={key} exact path={value.path}
                        render={props => (
                          <value.component {...props} routes={value.routes} />
                        )} />
                    } else {
                      // 向子元件傳值
                      return <Route key={key} path={value.path}
                        render={props => (
                          <value.component {...props} routes={value.routes} />
                        )} />
                    }
                  })
                }
              </Content>
            </Layout>
          </Layout>


        </div>
      </Router>
    )
  }
}
// ReactDOM.render(<SiderDemo />, mountNode);
export default App;

App.css

@import '~antd/dist/antd.css';
*{
  margin: 0;
  padding: 0;
}
.App .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color .3s;
}

.App .trigger:hover {
  color: #1890ff;
}

.App .logo {
  height: 32px;
  background: rgba(255,255,255,.2);
  margin: 16px;
}
.App .ant-menu-item a{
  color: #fff;
}