React新闻网站--Header组件拆分及样式布局

前端开发 作者: 2024-08-19 20:50:02
先放图,一个头部组件 index.js import React,{Component} from 'react'; import ReactDom from 'react-d
import React,{Component} from 'react';
import ReactDom from 'react-dom';
import { Layout } from 'antd';
import 'antd/dist/antd.css';
import './style.css';
import AppHeader from './components/AppHeader/';

const { Header,Footer,Content } = Layout;

class App extends Component{
    render(){
        return(
            <Layout style={{minWidth:1200}}>
                <Header className="header"><AppHeader/></Header>
                <Content className="content">Content</Content>
                <Footer className="footer">Footer</Footer>
            </Layout>
        )
    }
}

ReactDom.render(<App/>,document.getElementById('root'));
.header{
    background-color: #fff;
    border-bottom:1px solid #999;
}
.content{
    min-height:600px;
}
.footer{
    text-align: center;
    border-top:1px solid #999;
}
import React,{Component,Fragment} from 'react';
import logo from './logo.png';
import { Menu } from 'antd';
import {MailOutlined} from '@ant-design/icons';
import axios from 'axios';
import { Icon } from '@ant-design/compatible';
  

class AppHeader extends Component{
    constructor(props){
        super(props);
        this.state={
            list:[]
        }
    }

    componentDidMount(){
        axios.get("http://www.dell-lee.com/react/api/header.json")
        .then(res=>{
            console.log(res.data.data);
            this.setState({
                list:res.data.data
            })
        })
    }

    createMenuItem(){
        return this.state.list.map(item=>{
            (
                <Menu.Item key={item.id}>
                    <Icon type={item.icon} />
                    {item.title}
                </Menu.Item>
            )
        })
    }

    render(){
        (
            <Fragment>
                <img src={logo} className="logo" />
                <Menu mode="horizontal" className="menu">{this.createMenuItem()}</Menu>
            </Fragment>
        )
    }
}

export default AppHeader;
.logo{
    height:45px;
    float:left;
    margin-top:10px;
}
.menu{
    margin-left:40px !important;45px !important;none !important;
}
import { Icon,Button } from 'antd';

const Demo = () => (
  <div>
    <Icon type="smile" />
    <Button icon="smile" />
  </div>
);
 import { Button } from 'antd';

  // tree-shaking supported
- import { Icon } from 'antd';
+ import { SmileOutlined } from '@ant-design/icons';

  const Demo = () => (
    <div>
-     <Icon type="smile" />
+     <SmileOutlined />
      <Button icon={<SmileOutlined />} />
    </div>
  );

   or directly import
  import SmileOutlined from '@ant-design/icons/SmileOutlined';
import { Button } from 'antd' (
  <div>
    <Icon type="smile" />
    <Button icon="smile" />
  </div>
);
当前项目文件夹下要 cnpm install --save @ant-design/compatible

总结

原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65138.html