React: Route Task

  1. Add Routes to load “Users” and “Courses” on different pages (by
    entering a URL, without Links)

使用BroswerRouter讲index.js的render内容包裹住

ReactDOM.render(
<BrowserRouter>
    <App />
</BrowserRouter>, document.getElementById('root'));

在app.js中设置两个Route标签,其component分别为users和courses

<Route path="/users" exact component={Users}/>
<Route path="/courses" component={Courses}/>
  1. Add a simple navigation with two links => One leading to “Users”,
    one leading to “Courses”

在需要点击事件的地方加入Link,设置to参数,其地址与route的path一一对应

        <nav>
          <ul>
            <li><NavLink to="/users">Users</NavLink></li>
            <li><NavLink to="/courses">Courses</NavLink></li>
          </ul>
        </nav>
  1. Make the courses in “Courses” clickable by adding a link and load
    the “Course” component in the place of “Courses” (without passing
    any data for now)

在Courses中加入link,并且to为courses的url+其id

  let obj={
                                pathname:this.props.match.url+'/'+course.id,
                                id:course.id,
                                title:course.title
                            }
                                <Link to={obj}>
                                    <article 
                                        className="Course" 
                                        key={course.id}>{course.title}
                                    </article>
                                </Link>
  1. Pass the course ID to the “Course” page and output it there

方法1:通过Route的通配符传入,由props.match.params.id得到
此方法会使得所有匹配的route都被render;

<Route 
  path={this.props.match.url+'/:id'} 
  component={Course}
/>

方法2:通过在link的to中传入obj,向下传入参数,在props.location中提取

                <div>
                    <h1>{this.props.location.title}</h1>
                    <p>You selected the Course with ID: {this.props.location.id}</p>
                </div>
  1. Pass the course title to the “Course” page - pass it as a param or
    score bonus points by passing it as query params (you need to
    manually parse them though!)
  2. Load the “Course” component as a nested component of “Courses”

在Courses中设置course的地址——已做到
7. Add a 404 error page and render it for any unknown routes

**使用< Route component={} />即可设定4040返回 **

<Route component={ErrorPage}/> 
  1. Redirect requests to /all-courses to /courses (=> Your “Courses”
    page)

使用< Redirect /> 即可

           <Redirect from='/all-course' to='/courses'/> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React路由重定向是通过使用Redirect组件来实现的。在React中,我们可以使用render属性来指定要渲染的内容。通过将Redirect组件放置在一个Route组件内部,并将to属性设置为要重定向的路由地址,就可以实现路由重定向的功能。例如,在下面的代码中,当访问根路径"/"时,将会进行路由重定向,跳转到"/home"路由。 ```javascript import { BrowserRouter as Router, Route, Redirect } from "react-router-dom"; import Home from "./pages/Home"; import CityList from "./pages/CityList"; function App() { return ( <Router> <div className="App"> {/* 默认路由,实现路由的重定向 */} <Route exact path="/" render={() => <Redirect to="/home" />} /> <Route path="/home" component={Home} /> <Route path="/citylist" component={CityList} /> </div> </Router> ); } export default App; ``` 这样,当进入页面时,默认路由会重定向到"/home",然后匹配到Home组件进行渲染。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [react中路由重定向](https://blog.csdn.net/weixin_43131046/article/details/120565092)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [<React-Router>超全react路由知识点(跳转、传参、子路由、重定向等)](https://blog.csdn.net/m0_48465196/article/details/129184085)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值