欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

深入了解React(十九、路由5-带参数1 )

程序员文章站 2024-02-13 17:14:46
...
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter as Router, Route, Link} from 'react-router-dom';

import 'font-awesome/css/font-awesome.min.css';
import './index.css';
import './index.scss';

class A extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>
                <h1>Component A</h1>
                <p>参数:{this.props.match.params.id}</p>
            </div>
        )
    }
}

class B extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>Component B</div>
        )
    }
}

class Wrapper extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>
                <p><Link to="/a">组件A</Link></p>
                <p><Link to="/a/123">带参数的组件A</Link></p>
                <p><Link to="/b">组件B</Link></p>
                {this.props.children}
            </div>
        )
    }
}

ReactDOM.render(
    <Router>
        <Wrapper>
            <Route path="/a/:id" component={A}></Route>
            <Route path="/b" component={B}></Route>
        </Wrapper>
    </Router>,
    document.getElementById('app')
);