首页 文章

查询字符串react-router路径

提问于
浏览
1

我正在使用react-router 3.0.2并尝试使用查询字符串配置路由器路径 . 这就是我配置路由器的方式:

<Router history={browserHistory}>
                <Route path="abc/login.do" component={LoginComponent}/>
                <Route path="abc/publicLoginID.do?phase=def" component={VerifyComponent} />
                <Route path="abc/publicLoginID.do?phase=ghi" component={ImageComponent}/>
                <Route path="*" component={LoginComponent}/>
</Router>

我知道这不是在"Route"中指定查询字符串(?)的正确方法 . 如何确保每当用户输入“http://localhost:3000/abc/publicLoginID.do?phase=def " in the url, "验证组件" shows up, and when he enters " http://localhost:3000/abc/publicLoginID.do?phase=ghi " in the url, " ImageComponent”时 .

我在这里检查了一些案例:react-router match query stringQuerystring in react-router,但无法弄清楚如何使其工作 .

1 回答

  • 2

    您可以编写一个包装器组件,它将根据查询参数切换要呈现的内容

    <Router history={browserHistory}>
       <Route path="abc/login.do" component={LoginComponent}/>
       <Route path="abc/publicLoginID.do" component={WrapperComponent} />
       <Route path="*" component={LoginComponent}/>
    </Router>
    
    //WrapperComponent
    
    WrapperComponent = (props) => {
       if (props.location.query.phase==="def"){return <VerifyComponent {...props}/>}
       if (props.location.query.phase==="ghi"){return <ImageComponent {...props}/>}
    }
    

相关问题