美文网首页
React入门(6)- React-Router

React入门(6)- React-Router

作者: 申_9a33 | 来源:发表于2021-02-21 12:54 被阅读0次

    React-Router

    安装 npm i react-router-dom

    Router 渲染内容三种方式

    • children:func 优先级最高,不管location是否匹配,都会渲染
    • component 优先级次之
    • render:func *优先级最低

    src\page\RouterPage.js

    import React, { Component } from 'react'
    import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom';
    
    export default class RouterPage extends Component {
        render() {
            return (
                <div>
                    <h3>RouterPage</h3>
    
                    <Router>
                        <Link to="/">首页</Link>
                        <Link to="/user">用户中心</Link>
    
    
                        <Switch>
                            {/* exact 实现精确匹配 */}
                            <Route exact
                                path="/"
                                component={Home}  // 2
                            // children={() => <div>children</div>} // 1
                            // render={() => <div>render</div>} // 3
                            />
    
                            <Route
                                path="/user"
                                component={User} />
    
                            <Route
                                component={Empty} />
                        </Switch>
    
                    </Router>
                </div>
            )
        }
    }
    
    
    function Home() {
        return <div>
            <h3>Home Page</h3>
        </div>
    }
    
    function User(params) {
        return <div>
            <h3>User Page</h3>
        </div>
    }
    
    function Empty(params) {
        return <div>
            <h3>Empty Page</h3>
        </div>
    }
    

    相关文章

      网友评论

          本文标题:React入门(6)- React-Router

          本文链接:https://www.haomeiwen.com/subject/zwrnxltx.html