简体   繁体   English

如何在reactjs的子组件中进行动态路由?

[英]how to do dynamic routing in child component in reactjs?

In my case, i have 6 menu in my application.就我而言,我的应用程序中有 6 个菜单。 In header part i have 'Next' and 'back' buttons.在标题部分,我有“下一步”和“后退”按钮。 If i click next button, it should navigate to next menu, for back button, it should navigate to previous page.如果我点击下一步按钮,它应该导航到下一个菜单,对于后退按钮,它应该导航到上一页。 and header.js is a seperate component. header.js 是一个单独的组件。 Code is attached below代码附在下面

app.js应用程序.js

render() {
        return (
            <Router>
                <div className="h-100">
                    <Header />
                    <ContentNavigation />
                        <Switch>
                            <Route exact path='/' component={WelcomeMenu} />
                            <Route path='/principal' component={PrincipleMenu} />
                            <Route path='/mycar' component={MycarMenu} />
                            <Route path='/abc' component={abcMenu} />
                            <Route path='/ijkmenu' component={ijkMenu} />
                            <Route path='/xyzmenu' component={xyzMenu} />
                        </Switch>

                </div>
            </Router>
        );
    }

header.js头文件.js

render() {
        return (
                   <div className="header-right text-right header-text-pd-tp">
                    <span>
                         <i className ="icon-arrow-left prev-next-icon prev-icon"></i> 
                         BACK 
                   </span>
                    Current Chapter
                        <span> 
                         NEXT
                        <i className="icon-arrow-right prev-next-icon next-icon" aria-hidden="true"></i> 
                       </span>
                    </div>
       )
    }

This is one way to do routing这是进行路由的一种方式

this.props.history.push({
                pathname: '/your_route_path'
            })

Multiple ways- you can navigate to other component.多种方式 - 您可以导航到其他组件。

1 -using Link 1 - 使用Link

import {Link} from 'react-router-dom'

<Link to="/ijkmenu"> 
      NEXT
    <i className="icon-arrow-right prev-next-icon next-icon" aria-hidden="true"></i> 
</Link>

2 -using this.props.history.push() 2 - 使用this.props.history.push()

<span onClick={()=> this.props.history.push('/ijkmenu')}> 
          NEXT
        <i className="icon-arrow-right prev-next-icon next-icon" aria-hidden="true"></i> 
   </span>

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

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