全局布局

在上一小节里面我们知道,存在 ./src/layouts/index.js 时,会自动使用它作为全局布局。

第一步 打开./src/layouts/index.js

  1. import styles from './index.css';
  2. function BasicLayout(props) {
  3. return (
  4. <div className={styles.normal}>
  5. <h1 className={styles.title}>Yay! Welcome to umi!</h1>
  6. { props.children }
  7. </div>
  8. );
  9. }
  10. export default BasicLayout;

使用create-umi创建的项目,layout文件如上。可以看到,其实layout也是一个组件。 umi会自动使用它包裹页面,并传入props

  1. //props
  2. {
  3. history: {length: 1, action: "POP", location: {…}, createHref: ƒ, push: ƒ, …}
  4. location: {pathname: "/", search: "", hash: "", query: {…}, state: undefined}
  5. match: {path: "/", url: "/", isExact: true, params: {…}}
  6. route: {path: "/", component: ƒ, routes: Array(2), _title: "hero", _title_default: "hero"}
  7. staticContext: undefined
  8. }

你可以随意的修改这个文件,但是一定要记得包裹 { props.children }

第二步 使用antd的layout

在文件头部引入antd的Layout组件,并从Layout中取出Header, Content, Footer

  1. import { Layout } from 'antd';
  2. const { Header, Content, Footer } = Layout;

使用Header, Content, Footer,包裹children。

  1. import styles from './index.css';
  2. import { Layout } from 'antd';
  3. const { Header, Content, Footer } = Layout;
  4. function BasicLayout(props) {
  5. return (
  6. <Layout>
  7. <Header>
  8. <div style={{ color: 'white' }}>王者荣耀资料库</div>
  9. </Header>
  10. <Content style={{ padding: '0 50px' }}>
  11. {props.children}
  12. </Content>
  13. <Footer style={{ textAlign: 'center' }}>Umi 入门教程 Created by xiaohuoni</Footer>
  14. </Layout>
  15. );
  16. }
  17. export default BasicLayout;

WX20181205-165732@2x.png | center | 747x464

更改完后的效果,看起来不错吧,你可以随意的改改,看看效果。

第三步 增加页面导航

在头部引入antd的menu组件

  1. - import { Layout } from 'antd';
  2. + import { Layout, Menu } from 'antd';

在Header中增加导航

  1. <Header>
  2. - <div style={{ color: 'white' }}>王者荣耀资料库</div>
  3. + <div className={styles.logo}>王者荣耀资料库 </div>
  4. + <Menu
  5. + theme="dark"
  6. + mode="horizontal"
  7. + defaultSelectedKeys={['1']}
  8. + style={{ lineHeight: '64px' }}
  9. + >
  10. + <Menu.Item key="1">英雄</Menu.Item>
  11. + <Menu.Item key="2">局内道具</Menu.Item>
  12. + <Menu.Item key="3">召唤师技能</Menu.Item>
  13. + </Menu>
  14. </Header>

在./src/layouts/index.css中增加logo样式

  1. .logo {
  2. width: 148px;
  3. color: white;
  4. margin: 16px 24px 16px 0;
  5. float: left;
  6. font-size: 18px;
  7. line-height: 30px;
  8. }

为了使得页面更美观,修改Content

  1. <Content style={{ padding: '0 50px' }}>
  2. <div style={{ background: '#fff', padding: 24, minHeight: 280 }}>
  3. {props.children}
  4. </div>
  5. </Content>

最终效果

WX20181205-171137@2x.png | center | 747x507

代码回顾:这节课的全部代码