君宝宝 發表於 2019-9-9 10:46:00

React-router5.x 路由的使用及配置

<p>在 React router 中通常使用的组件有三种:</p>
<ul>
<li>路由组件(作为根组件): BrowserRouter(history模式) 和 HashRouter(hash模式)</li>
<li>路径匹配组件: Route 和 Switch</li>
<li>导航组件: Link 和 NavLink</li>
</ul>
<p>关于路由组件,如果我们的应用有服务器响应web的请求,建议使用<code>&lt;BrowserRouter&gt;</code>组件; 如果使用静态文件服务器,建议使用<code>&lt;HashRouter&gt;</code>组件</p>
<h2 id="1-安装">1. 安装</h2>
<pre><code>npm install react-router-dom
</code></pre>
<h2 id="2-实例">2. 实例</h2>
<pre><code>import React, { Component, Fragment } from 'react';
import { Provider } from 'react-redux';
import { BrowserRouter, Route } from 'react-router-dom';
import store from './store';
import Header from './common/header';
import Home from './pages/home';
import Detail from './pages/detail';
import Login from './pages/login';


class App extends Component {
render() {
    return (
      &lt;Provider store={store}&gt;
      &lt;Fragment&gt;
          &lt;BrowserRouter&gt;
            &lt;div&gt;
            &lt;Header /&gt;
            &lt;Route path='/' exact component={Home}&gt;&lt;/Route&gt;
            &lt;Route path='/login' exact component={Login}&gt;&lt;/Route&gt;
            &lt;Route path='/detail/:id' exact component={Detail}&gt;&lt;/Route&gt;
            &lt;/div&gt;
          &lt;/BrowserRouter&gt;
      &lt;/Fragment&gt;
      &lt;/Provider&gt;
    )
}
}

export default App;
</code></pre>
<h2 id="3-路由组件-browserrouter-和-hashrouter">3. 路由组件 BrowserRouter 和 HashRouter</h2>
<p>BrowserRouter(history模式) 和 HashRouter(hash模式)作为路由配置的最外层容器,是两种不同的模式,可根据需要选择。</p>
<h4 id="history-模式">history 模式:</h4>
<pre><code>class App extends Component {
render() {
    return (
      &lt;BrowserRouter&gt;
          &lt;Header /&gt;
          &lt;Route path='/' exact component={Home}&gt;&lt;/Route&gt;
          &lt;Route path='/login' exact component={Login}&gt;&lt;/Route&gt;
          &lt;Route path='/detail/:id' exact component={Detail}&gt;&lt;/Route&gt;
      &lt;/BrowserRouter&gt;
    )
}
}
</code></pre>
<h4 id="hash-模式">hash 模式:</h4>
<pre><code>class App extends Component {
render() {
    return (
      &lt;HashRouter&gt;
          &lt;Header /&gt;
          &lt;Route path='/' exact component={Home}&gt;&lt;/Route&gt;
          &lt;Route path='/login' exact component={Login}&gt;&lt;/Route&gt;
          &lt;Route path='/detail/:id' exact component={Detail}&gt;&lt;/Route&gt;
      &lt;/HashRouter&gt;
    )
}
}
</code></pre>
<h2 id="4-路径匹配组件-route-和-switch">4. 路径匹配组件: Route 和 Switch</h2>
<h3 id="一route-用来控制路径对应显示的组件">一、Route: 用来控制路径对应显示的组件</h3>
<p>有以下几个参数:</p>
<h4 id="41-path指定路由跳转路径">4.1 path:指定路由跳转路径</h4>
<h4 id="42-exact-精确匹配路由">4.2 exact: 精确匹配路由</h4>
<h4 id="43-component路由对应的组件">4.3 component:路由对应的组件</h4>
<pre><code>import About from './pages/about';

··· ···

&lt;Route path='/about' exact component={About}&gt;&lt;/Route&gt;
</code></pre>
<h4 id="44-render-通过写render函数返回具体的dom">4.4 render: 通过写render函数返回具体的dom:</h4>
<pre><code>&lt;Route path='/about' exact render={() =&gt; (&lt;div&gt;about&lt;/div&gt;)}&gt;&lt;/Route&gt;
</code></pre>
<p>render 也可以直接返回 About 组件,像下面:</p>
<pre><code>&lt;Route path='/about' exact render={() =&gt; &lt;About /&gt; }&gt;&lt;/Route&gt;
</code></pre>
<h5 id="但是这样写的好处是不仅可以通过-render-方法传递-props-属性并且可以传递自定义属性">但是,这样写的好处是,不仅可以通过 render 方法传递 props 属性,并且可以传递自定义属性:</h5>
<pre><code>&lt;Route path='/about' exact render={(props) =&gt; {
    return &lt;About {...props} name={'cedric'} /&gt;
}}&gt;&lt;/Route&gt;
</code></pre>
<p>然后,就可在 About 组件中获取 props 和 name 属性:</p>
<pre><code>componentDidMount() {
    console.log(this.props)
}


// this.props:
// history: {length: 9, action: "POP", location: {…}, createHref: ƒ, push: ƒ, …}
// location: {pathname: "/home", search: "", hash: "", state: undefined, key: "ad7bco"}
// match: {path: "/home", url: "/home", isExact: true, params: {…}}
// name: "cedric"
</code></pre>
<h5 id="render-方法也可用来进行权限认证">render 方法也可用来进行权限认证:</h5>
<pre><code>&lt;Route path='/user' exact render={(props) =&gt; {
    // isLogin 从 redux 中拿到, 判断用户是否登录
    return isLogin ? &lt;User {...props} name={'cedric'} /&gt; : &lt;div&gt;请先登录&lt;/div&gt;
}}&gt;&lt;/Route&gt;
</code></pre>
<h3 id="45-location-将--与当前历史记录位置以外的位置相匹配则此功能在路由过渡动效中非常有用">4.5 location: 将 <route> 与当前历史记录位置以外的位置相匹配,则此功能在路由过渡动效中非常有用</route></h3>
<h3 id="46-sensitive是否区分路由大小写">4.6 sensitive:是否区分路由大小写</h3>
<h3 id="47-strict-是否配置路由后面的-">4.7 strict: 是否配置路由后面的 '/'</h3>
<h3 id="二switch">二、Switch</h3>
<p>渲染与该地址匹配的第一个子节点 <code>&lt;Route&gt;</code> 或者 <code>&lt;Redirect&gt;</code>。</p>
<p>类似于选项卡,只是匹配到第一个路由后,就不再继续匹配:</p>
<pre><code>&lt;Switch&gt;
    &lt;Route path='/home'component={Home}&gt;&lt;/Route&gt;
    &lt;Route path='/login'component={Login}&gt;&lt;/Route&gt;
    &lt;Route path='/detail'component={detail}&gt;&lt;/Route&gt;
    &lt;Redirect to="/home" from='/' /&gt;
&lt;/Switch&gt;

// 类似于:
// switch(Route.path) {
//   case '/home':
//         return Home
//   case '/login':
//         return Login
//   ··· ···
// }
</code></pre>
<p>所以,如果像下面这样:</p>
<pre><code>&lt;Switch&gt;
    &lt;Route path='/home'component={Home}&gt;&lt;/Route&gt;
    &lt;Route path='/login'component={Login}&gt;&lt;/Route&gt;
    &lt;Route path='/detail'component={detail}&gt;&lt;/Route&gt;
    &lt;Route path='/detail/:id'component={detailId}&gt;&lt;/Route&gt;
    &lt;Redirect to="/home" from='/' /&gt;
&lt;/Switch&gt;

</code></pre>
<p>当路由为<code>/detail/1</code>时,只会访问匹配组件<code>detail</code>, 所以需要在detail路由上加上<code>exact</code>:</p>
<pre><code>&lt;Switch&gt;
    &lt;Route path='/home'component={Home}&gt;&lt;/Route&gt;
    &lt;Route path='/login'component={Login}&gt;&lt;/Route&gt;
    &lt;Route path='/detail' exactcomponent={detail}&gt;&lt;/Route&gt;
    &lt;Route path='/detail/:id'component={detailId}&gt;&lt;/Route&gt;
    &lt;Redirect to="/home" from='/' /&gt;
&lt;/Switch&gt;

</code></pre>
<h5 id="注意如果路由-route-外部包裹-switch-时路由匹配到对应的组件后就不会继续渲染其他组件了但是如果外部不包裹-switch-时所有路由组件会先渲染一遍然后选择到匹配的路由进行显示">注意:如果路由 Route 外部包裹 Switch 时,路由匹配到对应的组件后,就不会继续渲染其他组件了。但是如果外部不包裹 Switch 时,所有路由组件会先渲染一遍,然后选择到匹配的路由进行显示。</h5>
<h2 id="5-导航组件-link-和-navlink">5. 导航组件: Link 和 NavLink</h2>
<p>Link 和 NavLink 都可以用来指定路由跳转,NavLink 的可选参数更多。</p>
<h3 id="link">Link</h3>
<p>两种配置方式:</p>
<h4 id="通过字符串执行跳转路由">通过字符串执行跳转路由</h4>
<pre><code>&lt;Link to='/login'&gt;
    &lt;span&gt;登录&lt;/span&gt;
&lt;/Link&gt;
</code></pre>
<h4 id="通过对象指定跳转路由">通过对象指定跳转路由</h4>
<ul>
<li>pathname: 表示要链接到的路径的字符串。</li>
<li>search: 表示查询参数的字符串形式。</li>
<li>hash: 放入网址的 hash,例如 #a-hash。</li>
<li>state: 状态持续到 location。通常用于隐式传参(埋点),可以用来统计页面来源</li>
</ul>
<pre><code>&lt;Link to={{
      pathname: '/login',
      search: '?name=cedric',
      hash: '#someHash',
      state: { fromWechat: true }
    }}&gt;
    &lt;span&gt;登录&lt;/span&gt;
&lt;/Link&gt;
</code></pre>
<p>点击链接 进入 Login 页面后,就可以在<code>this.props.location.state</code>中看到 <code>fromWechat: true</code>:</p>
<h3 id="navlink">NavLink</h3>
<p>可以看做 一个特殊版本的 Link,当它与当前 URL 匹配时,为其渲染元素添加样式属性。</p>
<pre><code>&lt;Link to='/login' activeClassName="selected"&gt;
    &lt;span&gt;登录&lt;/span&gt;
&lt;/Link&gt;
</code></pre>
<pre><code>&lt;NavLink
to="/login"
activeStyle={{
    fontWeight: 'bold',
    color: 'red'
   }}
&gt;
    &lt;span&gt;登录&lt;/span&gt;
&lt;/NavLink&gt;
</code></pre>
<ul>
<li>exact: 如果为 true,则仅在位置完全匹配时才应用 active 的类/样式。</li>
<li>strict: 当为 true,要考虑位置是否匹配当前的URL时,pathname 尾部的斜线要考虑在内。</li>
<li>location 接收一个location对象,当url满足这个对象的条件才会跳转</li>
<li>isActive: 接收一个回调函数,只有当 active 状态变化时才能触发,如果返回false则跳转失败</li>
</ul>
<pre><code>const oddEvent = (match, location) =&gt; {
if (!match) {
    return false
}
const eventID = parseInt(match.params.eventID)
return !isNaN(eventID) &amp;&amp; eventID % 2 === 1
}

&lt;NavLink
to="/login"
isActive={oddEvent}
&gt;login&lt;/NavLink&gt;
</code></pre>
<h2 id="6-redirect">6. Redirect</h2>
<p><code>&lt;Redirect&gt;</code> 将导航到一个新的地址。即重定向。</p>
<pre><code>&lt;Switch&gt;
    &lt;Route path='/home' exact component={Home}&gt;&lt;/Route&gt;
    &lt;Route path='/login' exact component={Login}&gt;&lt;/Route&gt;
    &lt;Redirect to="/home" from='/' exact /&gt;
&lt;/Switch&gt;
</code></pre>
<p>上面,当访问路由<code>‘/’</code>时,会直接重定向到<code>‘/home’</code>。</p>
<p><code>&lt;Redirect&gt;</code> 常在用户是否登录:</p>
<pre><code>class Center extends PureComponent {
    render() {
      const { loginStatus } = this.props;
      if (loginStatus) {
            return (
                &lt;div&gt;个人中心&lt;/div&gt;
            )
      } else {
            return &lt;Redirect to='/login' /&gt;
      }
    }
}
</code></pre>
<p>也可使用对象形式:</p>
<pre><code>&lt;Redirect
to={{
    pathname: "/login",
    search: "?utm=your+face",
    state: { referrer: currentLocation }
}}
/&gt;
</code></pre>
<h2 id="7-withrouter">7. withRouter</h2>
<p>withRouter 可以将一个非路由组件包裹为路由组件,使这个非路由组件也能访问到当前路由的match, location, history对象。</p>
<pre><code>import { withRouter } from 'react-router-dom';

class Detail extends Component {
    render() {
      ··· ···
    }
}

const mapStateToProps = (state) =&gt; {
    return {
      ··· ···
    }
}

const mapDispatchToProps = (dispatch) =&gt; {
    return {
      ··· ···
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(withRouter(Detail));
</code></pre>
<h2 id="8-编程式导航---history-对象">8. 编程式导航 - history 对象</h2>
<p>例如,点击img进入登录页:</p>
<pre><code>class Home extends PureComponent {

    goHome = () =&gt; {
      console.log(this.props);
      
      this.props.history.push({
            pathname: '/login',
            state: {
                identityId: 1
            }
      })
    }

    render() {
      return (
            &lt;img className='banner-img' alt='' src="img.png" onClick={this.goHome} /&gt;
      )
    }
}
</code></pre>
<p>history 对象通常会具有以下属性和方法:</p>
<pre><code>length - (number 类型) history 堆栈的条目数
action - (string 类型) 当前的操作(PUSH, REPLACE, POP)
location - (object 类型) 当前的位置。location 会具有以下属性:
pathname - (string 类型) URL 路径
search - (string 类型) URL 中的查询字符串
hash - (string 类型) URL 的哈希片段
state - (object 类型) 提供给例如使用 push(path, state) 操作将 location 放入堆栈时的特定 location 状态。只在浏览器和内存历史中可用。
push(path, ) - (function 类型) 在 history 堆栈添加一个新条目
replace(path, ) - (function 类型) 替换在 history 堆栈中的当前条目
go(n) - (function 类型) 将 history 堆栈中的指针调整 n
goBack() - (function 类型) 等同于 go(-1)
goForward() - (function 类型) 等同于 go(1)
block(prompt) - (function 类型) 阻止跳转。
</code></pre>
<h4 id="注意只有通过-route-组件渲染的组件才能在-thisprops-上找到-history-对象">注意,只有通过 Route 组件渲染的组件,才能在 this.props 上找到 history 对象</h4>
<p>所以,如果想在路由组件的子组件中使用 history ,需要使用 withRouter 包裹:</p>
<pre><code>import React, { PureComponent } from 'react';
import { withRouter } from 'react-router-dom';

class 子组件 extends PureComponent {

    goHome = () =&gt; {
      this.props.history.push('/home')
    }


    render() {
      console.log(this.props)
      return (
            &lt;div onClick={this.goHome}&gt;子组件&lt;/div&gt;
      )
    }
}

export default withRouter(子组件);
</code></pre>
<h2 id="9-路由过渡动画">9. 路由过渡动画</h2>
<pre><code>import { TransitionGroup, CSSTransition } from "react-transition-group";

class App extends Component {

render() {
    return (
      &lt;Provider store={store}&gt;
      &lt;Fragment&gt;
          &lt;BrowserRouter&gt;
            &lt;div&gt;
            &lt;Header /&gt;
            
            {/* 最外部的&lt;Route&gt;&lt;/Route&gt;不进行任何路由匹配,仅仅是用来传递 location */}
            
            &lt;Route render={({location}) =&gt; {
                console.log(location);
                return (
                  &lt;TransitionGroup&gt;
                  &lt;CSSTransition
                      key={location.key}
                      classNames='fade'
                      timeout={300}
                  &gt;
                      &lt;Switch&gt;
                        &lt;Redirect exact from='/' to='/home' /&gt;
                        &lt;Route path='/home' exact component={Home}&gt;&lt;/Route&gt;
                        &lt;Route path='/login' exact component={Login}&gt;&lt;/Route&gt;
                        &lt;Route path='/write' exact component={Write}&gt;&lt;/Route&gt;
                        &lt;Route path='/detail/:id' exact component={Detail}&gt;&lt;/Route&gt;
                        &lt;Route render={() =&gt; &lt;div&gt;Not Found&lt;/div&gt;} /&gt;
                      &lt;/Switch&gt;
                  &lt;/CSSTransition&gt;
                  &lt;/TransitionGroup&gt;
                )
            }}&gt;
            &lt;/Route&gt;
            &lt;/div&gt;
          &lt;/BrowserRouter&gt;
      &lt;/Fragment&gt;
      &lt;/Provider&gt;
    )
}
}

</code></pre>
<pre><code>.fade-enter {
opacity: 0;
z-index: 1;
}

.fade-enter.fade-enter-active {
opacity: 1;
transition: opacity 300ms ease-in;
}
</code></pre>
<h2 id="10-打包部署的路由配置">10. 打包部署的路由配置</h2>
<p>项目执行<code>npm run build</code>后,将打包后的build文件当大 Nginx 配置中。</p>
<p>如果 react-router 路由 使用了 history 模式(即<code>&lt;BrowserRouter&gt;</code>),那么在 Nginx 配置中必须加上:</p>
<pre><code>location / {
      ··· ···
      try_files $uri /index.html;
      ··· ···
      }
    }
</code></pre>
<p>如果 react-router 路由 使用了 hash 模式(即<code>&lt;HashRouter&gt;</code>),那么在 Nginx 中不需要上面的配置。</p><br><br>
来源:https://www.cnblogs.com/cckui/p/11490372.html
頁: [1]
查看完整版本: React-router5.x 路由的使用及配置