react-router 实例

来源:互联网 发布:成都买车位划算 知乎 编辑:程序博客网 时间:2024/06/03 20:33
使用理由:react 本身并没有路由功能,所以必须借助于第三方的库,也是react推荐的 react-router ,所以我就没有拒绝的理由。
我们就开始吧!
首先,中文文档上提供的内容我就不复制了,直接看代码。
代码顺序是我的顺序,将就着看吧!

第一步: 先从 ant.design 中选择了一个 MENU 的组件,用来配合 router 使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Menu.jsx 
import React, { Component } from 'react';
import { Menu, Icon } from 'antd';
//  ant 
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
//  ant.design  MENU 
const MenuListDom = React.createClass({
    getInitialState() {
        return {
            current: '1',
            openKeys: []
        };
    },
    handleClick(e) {
        window.location.hash = e.key; // ‘key’‘about’
        this.setState({
            current: e.key,
            openKeys: e.keyPath.slice(1)
        });
    },
    onToggle(info) {
        console.log('onToggle', info)
        this.setState({
            openKeys: info.open ? info.keyPath : info.keyPath.slice(1)
        });
    },
    render: function(){
        return (
            < Menu onClick = {this.handleClick}
                style = {{width: 240}}
                openKeys = {this.state.openKeys}
                onOpen = {this.onToggle}
                onClose = {this.onToggle}
                selectedKeys = {[this.state.current]}
                mode = "inline"
            >
                < SubMenu key = "sub1" title = { 
                    < span > 
                        < Icon type = "mail" / > 
                        < span >  < /span>
                    </span >
                }>
                    < Menu.Item key = "about" >  /*about*/
                        1
                    < /Menu.Item> 
                    < Menu.Item key = "users" >
                        2
                    < /Menu.Item> 
                    < Menu.Item key = "3" > 3 < /Menu.Item> 
                    < Menu.Item key = "4" > 4 < /Menu.Item> 
                < /SubMenu> 
                < SubMenu key = "sub2" title = { 
                    < span > 
                        < Icon type = "appstore" / > 
                        < span >  < /span>
                    </span >
                } >
                    < Menu.Item key = "5" > 5 < /Menu.Item> 
                    < Menu.Item key = "6" > 6 < /Menu.Item> 
                    < SubMenu key = "sub3" title = "" >
                        < Menu.Item key = "7" > 7 < /Menu.Item> 
                        < Menu.Item key = "8" > 8 < /Menu.Item> 
                    < /SubMenu> 
                < /SubMenu> 
                < SubMenu key = "sub4" title = { 
                    < span > 
                        < Icon type = "setting" / > 
                        < span >  < /span>
                    </span >
                } >
                    < Menu.Item key = "9" > 9 < /Menu.Item> 
                    < Menu.Item key = "10" > 10 < /Menu.Item> 
                    < Menu.Item key = "11" > 11 < /Menu.Item> 
                    < Menu.Item key = "12" > 12 < /Menu.Item> 
                < /SubMenu> 
            < /Menu>
        )
    }
});
//  MenuList 
export default class MenuList extends Component {
    render() {
        return (
            <div> // body   div 
                <div id="react-menu">  // div
                    <div className="logo">Hell Logo</div>
                    <MenuListDom location={this.props.location} /> //  ant UI
                </div>
                <div id="react-content"> // 
                    {this.props.children}
                </div>
                <div id="react-top"></div> // 
            </div>
        );
    }
};

第二步:在 index.jsx 中配置路由
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// index.jsx 
import '../common/lib';
import ReactDOM from 'react-dom';
import React from 'react';
import MenuList from '../component/Menu';
import '../component/App.less';
import { Router, Route, Link, hashHistory, IndexRoute } from 'react-router';
//  About  Users
import { About } from '../component/master/About';
import { Users } from '../component/Users';
ReactDOM.render((
    <Router history={hashHistory}>
        <Route path="/" component={MenuList}>
            <IndexRoute component={About} />
            <Route path="about" component={About} />
            <Route path="users" component={Users} />
        </Route>
    </Router>
), document.getElementById("content"));

第三步:这次轮到 index.html 页面了
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
            position:absolute;
            top:0;
            left:0;
            bottom:0;
            background:#fff;
        }
        #react-top{
            height:50px;
            position:absolute;
            left:240px;
            top:0;
            right:0;
            background:#fff;
        }
        #react-content{
            position:absolute;
            top:50px;
            left:240px;
            bottom:0;
            right:0;
            padding:20px 0 0 20px;
            background:rgb(234,234,241)
        }
    </style>
</head>
<body>
<div id="content"></div>
</body>
<script src="./common.js"></script>
<script src="./index.js"></script>
</html>


第四步:看下效果
效果图
效果图2