zoukankan      html  css  js  c++  java
  • react-router

    import React from 'react'
    import ReactDOM from 'react-dom'
    import { HashRouter as Router, Route, Link, Switch } from 'react-router-dom'
    
    
    class Wrapper extends React.Component{
        constructor(props) {
            super(props);
        }
        render() {
            return <div>
                <Link to="/a">组件A</Link>
                <br/>
                <Link to="/a/123">带参数的组件A</Link>
                <br/>
                <Link to="/a/sub">/a/sub</Link>
                <br/>
                <Link to="/b">组件B</Link>
            {this.props.children}</div>
        }
    }
    class A extends React.Component{
        constructor (props) {
            super(props);
        }
        render() {
            return (
                <div>
                    Component A
                    参数是: {this.props.match.params.id}
                    <Switch>
                        <Route exact path={`${this.props.match.path}`}
                            render={ (route) => {
                                return <div>当前组件是bu带参数的A}</div>
                        }} />
                        <Route exact path={`${this.props.match.path}/sub`}
                            render={ (route) => {
                                return <div>当前组件是sub</div>
                        }} />
                        <Route path={`${this.props.match.path}/:id`}
                            render={ (route) => {
                                return <div>当前组件是带参数的A,参数是{route.match.params.id}</div>
                        }} />
                    </Switch>
                </div>
            )
        }
    }
    class B extends React.Component{
        constructor (props) {
            super(props);
        }
        render() {
            return <div> Component B</div>
        }
    }
    
    ReactDOM.render(
        <Router>
            <Wrapper>
                <Route path="/a" component={A} />
                <Route path="/b" component={B} />
            </Wrapper>
        </Router>,
      document.getElementById('app')
    );
  • 相关阅读:
    浏览器同源政策及其规避方法---转阮大神
    js跨域详解
    js中top、self、parent
    杂记
    DOM 踩踩踩
    java idea 连接数据库
    数据库mySQL常用命令
    用迭代实现80人围成一圈逢3取出
    如何把通过类建立的对象存入数组中.
    面向对象编程
  • 原文地址:https://www.cnblogs.com/xuyan1/p/9231525.html
Copyright © 2011-2022 走看看