zoukankan      html  css  js  c++  java
  • React-router4 第六篇 No Match 404

    https://reacttraining.com/react-router/web/example/no-match
    react-router-dom 又一个新属性 Switch

    在Switch 的包裹下, 如果不填写path属性,那么,所有未定义的路由都会渲染这个组件,顺便把路由传进去

    
    import React from 'react'
    import {
      BrowserRouter as Router,
      Route,
      Link,
      Switch,
      Redirect
    } from 'react-router-dom'
    
    const NoMatchExample = () => (
      <Router>
        <div>
          <ul>
            <li><Link to="/">Home</Link></li>
            <li><Link to="/old-match">Old Match, to be redirected</Link></li>
            <li><Link to="/will-match">Will Match</Link></li>
            <li><Link to="/will-not-match">Will Not Match</Link></li>
            <li><Link to="/also/will/not/match">Also Will Not Match</Link></li>
          </ul>
          <Switch>
            <Route path="/" exact component={Home}/>
            <Redirect from="/old-match" to="/will-match"/>
            <Route path="/will-match" component={WillMatch}/>
            <Route component={NoMatch}/>       // 所有没有定义的路由都会匹配NoMatch组件
          </Switch>
        </div>
      </Router>
    )
    
    const Home = () => (
      <p>
        A <code>&lt;Switch></code> renders the
        first child <code>&lt;Route></code> that
        matches. A <code>&lt;Route></code> with
        no <code>path</code> always matches.
      </p>
    )
    
    const WillMatch = () => <h3>Matched!</h3>
    
    const NoMatch = ({ location }) => (
      <div>
        <h3>No match for <code>{location.pathname}</code></h3>
      </div>
    )
    
    export default NoMatchExample
    
  • 相关阅读:
    C语言学习之指针
    IT人和普洱茶
    茶如人生 你是什么茶?
    普洱茶的冲泡技巧
    普洱茶保健功效
    廖雪峰Python总结3
    廖雪峰Python总结2
    Linux之软件包安装管理
    Linux常用命令6 关机重启命令
    Linux之Vim编辑器
  • 原文地址:https://www.cnblogs.com/daowangzhizhu-pt/p/6656227.html
Copyright © 2011-2022 走看看