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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值