简体   繁体   English

react-router 任意数量的路径参数

[英]react-router any number of paths params

I'm trying to create route that can handle urls like我正在尝试创建可以处理 urls 之类的路由

/results/create-order/OFR44159995010000/ /结果/创建订单/OFR44159995010000/

and

/results/create-order/OFR44159995010000/OFR44159995010001/ /结果/创建订单/OFR44159995010000/OFR44159995010001/

and

/results/create-order/OFR44159995010000/OFR44159995010001/OFR44159995010002 and so on, and i can't figure out how i can do this. /results/create-order/OFR44159995010000/OFR44159995010001/OFR44159995010002 等等,我不知道如何做到这一点。 my Route component looks like so for now:我的 Route 组件现在看起来像这样:

            <Route
                path="/results/create-order/:flightId(OFR\d+)"
                render={props => {
                    console.log(props);

                    return <div>here</div>;
                }}
            />

Instead of passing your flight id in your route you can pass it as array to your component.您可以将它作为数组传递给您的组件,而不是在您的路线中传递您的航班 ID。

  1. Create a simple route and pass your component to it创建一个简单的路由并将您的组件传递给它

    <Route path="/results/create-order)" render={YourComponent} />
  2. Then you can pass your flight ids as an array to your component然后您可以将您的航班 ID 作为数组传递给您的组件

    <Link to={{ pathname: '/results/create-order', state: { flightIds : [ pass your flight ids here ] } }}> My Link </Link>
  3. Access your ids in your component using: this.props.location.state.flightIds .使用: this.props.location.state.flightIds访问组件中的 id。

 <Route
            path="/results/create-order/:flightId"
            component={YourComponent}
        />

You just need to write like that and in your components, you will get flightId then write your business logic你只需要这样写,在你的组件中,你会得到 flightId 然后写你的业务逻辑

You don't need (OFR\\d+) in your path.您的路径中不需要(OFR\\d+) flightId will contain whatever id is passed. flightId将包含传递的任何id And you can access it using props.match.params.flightId .您可以使用props.match.params.flightId访问它。 The same goes for other Id's ( /:flightId/:anotherFlightId ), you just need to use different names for the Ids.其他 Id ( /:flightId/:anotherFlightId ) 也是如此,您只需要为 Id 使用不同的名称。

            <Route
                exact
                path="/results/create-order/:flightId"
                render={props => {
                    console.log(props.match.params.flightId); //logs flightId

                    return <div>here</div>;
                }}
            />
             <Route
                exact
                path="/results/create-order/:flightId/:anotherFlightId"
                render={props => {
                    console.log(props.match.params.anotherFlightId); //logs anotherFlightId

                    return <div>here</div>;
                }}
            />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM