就是官方文檔、各類案例嘗試寫了一遍
採用的是create-react-app配置安裝,並才考了一下文章進行初始化配置
文章地址: https://blog.csdn.net/Cress_z...
使用組件:react+react-router4.0+antd
相信看到該文檔的各位,其實對react已經有了瞭解或者比較熟悉了。u 因此這裏只是放一些主要的部分,其餘部分請github上下載查看。css
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import {BrowserRouter as Router,Route,Switch} from 'react-router-dom'; import registerServiceWorker from './registerServiceWorker'; import App from "./components/App/App"; import Login from "./components/login"; ReactDOM.render( <div> <Router> <Switch> <Route path="/login" component={Login} /> <Route path="/" component={App} /> </Switch> </Router> </div> , document.getElementById('root') ); registerServiceWorker();
import React, { Component } from 'react'; import './App.css'; import { Layout,Icon } from 'antd'; import RouteConfig from './../../router/index' import HomeMenu from './../Menu/homeMenu' import HeaderMenu from '../Header/header' //const { Header } = Layout; /** 0、獲取和傳遞組件值,用props 一、selHeaderMenu:同子組件headerjs進行通訊的函數,不須要在自身組件中定義,只須要在子組件中定義 二、子組件傳遞過來的menu值,經過selHeader函數進行改變 三、Appjs自身做爲父組件,向子組件homeMenujs傳遞menu值 **/ class App extends Component { state = { headerMenu : '', sliderMenu : '' } selHeader(menu,slider){ this.setState({headerMenu: menu,sliderMenu : slider}); } render() { var h = document.documentElement.clientHeight || document.body.clientHeight; var mainStyle = { height:h-60, overflowY:'auto' }; return ( <div> <Layout> <HeaderMenu selHeadrMenu={(menu,slider)=>this.selHeader(menu,slider)}></HeaderMenu> <div className='app-content' style={mainStyle}> <div className='app-left-menu'> <HomeMenu sliderMenu={this.state.headerMenu} sliderLeftMenu={this.state.sliderMenu}></HomeMenu> </div> <div className='app-right-menu'> <div style={{width:'75%'}}> <RouteConfig></RouteConfig> </div> {/*右側快捷菜單*/} <div className='app-right-left-content'> <div className='item-title'> 最近更新 </div> <div className='item-value'> <Icon type="file" />xxxxxxxxxxx </div> <div className='item-value'> <Icon type="file" />xxxxxxxxxxx </div> <div className='item-value'> <Icon type="file" />xxxxxxxxxxx </div> </div> </div> <div style={{clear:'both'}}></div> </div> </Layout> </div> ); } } export default App;
import React, { Component } from 'react'; import { Link } from 'react-router-dom'; import { Icon } from 'antd'; import './homeMenu.css' /** * 0、獲取和傳遞組件值,用props * 一、 */ class HomeMenu extends Component { state = { sliderMenu : '' } selSilder(silder) { this.setState({sliderMenu: silder}); //保存選中的位置記錄 localStorage.setItem("silderMenu", silder); } initState(){ let silder = localStorage.getItem("silderMenu"); this.setState({sliderMenu: silder ? silder : 'home'}); } render() { return ( <div style={{textAlign:'left',marginLeft:'70px'}}> {this.props.sliderMenu === 'menu1' && <ul className='menu-ul'> <Link to="/" className={this.state.sliderMenu === 'home' ? 'color-a5a5a5 font-size12 menu-active' : 'color-a5a5a5 font-size12'} onClick={() => this.selSilder('home')}> <li> <Icon type="home" className='menu-ul-icon'/>IT技術 </li> </Link> <Link to="/photography" className={this.state.sliderMenu === 'photography' ? 'color-a5a5a5 font-size12 menu-active' : 'color-a5a5a5 font-size12'} onClick={() => this.selSilder('photography')}> <li> <Icon type="home" className='menu-ul-icon'/>攝影、遊記 </li> </Link> <Link to="/music" className={this.state.sliderMenu === 'music' ? 'color-a5a5a5 font-size12 menu-active' : 'color-a5a5a5 font-size12'} onClick={() => this.selSilder('music')}> <li> <Icon type="home" className='menu-ul-icon'/>音樂筆記 </li> </Link> </ul>} {this.props.sliderMenu === 'menu2' && <ul className='menu-ul'> <Link to="/Home2" className={this.state.sliderMenu === 'home2' ? 'color-a5a5a5 font-size12 menu-active' : 'color-a5a5a5 font-size12'} onClick={() => this.selSilder('home2')}> <li> <Icon type="home" className='menu-ul-icon'/>主頁2 </li> </Link> <Link to="/About2" className={this.state.sliderMenu === 'about2' ? 'color-a5a5a5 font-size12 menu-active' : 'color-a5a5a5 font-size12'} onClick={() => this.selSilder('about2')}> <li> <Icon type="home" className='menu-ul-icon'/>關於咱們2 </li> </Link> <Link to="/News2" className={this.state.sliderMenu === 'news2' ? 'color-a5a5a5 font-size12 menu-active' : 'color-a5a5a5 font-size12'} onClick={() => this.selSilder('news2')}> <li> <Icon type="home" className='menu-ul-icon'/>新聞頁面2 </li> </Link> </ul>} </div> ); } //DOM加載完成後執行,同上面的註釋 componentDidMount() { this.initState(); } componentWillReceiveProps(){ this.initState(); } } export default HomeMenu
import React, { Component } from 'react'; import { Link } from 'react-router-dom'; import './header.css'; class HeaderMenu extends Component { state = { headActive:'menu1' }; selMenu(menu,silder){ /** * 獲取和傳遞組件值,用props * 流程爲:header選中後,獲得相應的值,通知父組件Appjs做出改變---->切換到Appjs中查看 * this.props.selHeadrMenu:表示向父組件Appjs的函數selHeadrMenu傳遞相關值 * this.setState({headActive: menu}):修改state的headActive選中值 */ this.props.selHeadrMenu(menu,silder); this.setState({headActive: menu}); //保存header選中的位置記錄 localStorage.setItem("headerMenu",menu); localStorage.setItem("silderMenu",silder); } render() { return ( <div className='head-class'> <div className='head-logo text-center'> logo </div> <div className='head-menu-main'> <div className='head-menu-item'> <Link to="/" > <span className={this.state.headActive === 'menu1' ? 'head-active head-menu-child-item' : 'head-menu-child-item'} onClick={() => this.selMenu('menu1','home')}> 菜單一 </span> </Link> <Link to="/home2"> <span className={this.state.headActive === 'menu2' ? 'head-active head-menu-child-item' : 'head-menu-child-item'} onClick={() => this.selMenu('menu2','home2')}> 菜單二 </span> </Link> </div> <div style={{position:'absolute',right:'20px',top:'0px',color:'#a5a5a5'}}> admin </div> </div> <div style={{clear:'both'}}></div> </div> ); } //DOM加載完成後執行,同上面的註釋 componentDidMount() { let menu = localStorage.getItem("headerMenu"); let silder = localStorage.getItem("silderMenu"); this.props.selHeadrMenu(menu ? menu : 'menu1',silder ? silder : 'home'); this.setState({headActive: menu ? menu : 'menu1'}); } } export default HeaderMenu
主要代碼部分爲以上4個文件,其實很簡單,具體github地址:
https://github.com/522011796/...vue