地板油 發表於 2019-6-3 16:42:00

react 中的路由 Link 和Route和NavLink

<p>route是配置,link是使用</p>
<p>https://blog.csdn.net/chern1992/article/details/77186118(copy)</p>
<p>嵌套路由一般使用Route,类似于vue中的作为嵌套路由的渲染,可以直接通过固定路由进入某一局部,等同于局部切换</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> index.js</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">render((
</span>&lt;Router history={hashHistory}&gt;
    &lt;Route path="/" component={App}&gt;<span style="color: rgba(0, 0, 0, 1)">
      {</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 注意这里把两个子组件放在Route里嵌套在了App的Route里/}
      &lt;Route path="/repos" component={Repos}/&gt;
      &lt;Route path="/about" component={About}/&gt;
    &lt;/Route&gt;
&lt;/Router&gt;
), document.getElementById('app'))</span></pre>
</div>
<p>Link进行的是路由切换跳转,整个单页面已经切换,而且能知道指向的路径是否是一个有效的路由</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> modules/NavLink.js</span>
import React from 'react'<span style="color: rgba(0, 0, 0, 1)">
import { Link } from </span>'react-router'<span style="color: rgba(0, 0, 0, 1)">

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> React.createClass({
render() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;Link {...<span style="color: rgba(0, 0, 255, 1)">this</span>.props} activeClassName="active"/&gt;
<span style="color: rgba(0, 0, 0, 1)">}
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> modules/App.js</span>
import NavLink from './NavLink'

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>

&lt;li&gt;&lt;NavLink to="/about"&gt;About&lt;/NavLink&gt;&lt;/li&gt;
&lt;li&gt;&lt;NavLink to="/repos"&gt;Repos&lt;/NavLink&gt;&lt;/li&gt;</pre>
</div>
<p>&nbsp;</p>
<p>NavLink<br>&lt;NavLink&gt;是&lt;Link&gt;的一个特定版本,会在匹配上当前的url的时候给已经渲染的元素添加参数,组件的属性有</p>
<p>activeClassName(string):设置选中样式,默认值为active<br>activeStyle(object):当元素被选中时,为此元素添加样式<br>exact(bool):为true时,只有当导致和完全匹配class和style才会应用<br>strict(bool):为true时,在确定为位置是否与当前URL匹配时,将考虑位置pathname后的斜线<br>isActive(func)判断链接是否激活的额外逻辑的功能</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> activeClassName选中时样式为selected</span>
&lt;<span style="color: rgba(0, 0, 0, 1)">NavLink
to</span>="/faq"<span style="color: rgba(0, 0, 0, 1)">
activeClassName</span>="selected"
&gt;FAQs&lt;/NavLink&gt;

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 选中时样式为activeStyle的样式设置</span>
&lt;<span style="color: rgba(0, 0, 0, 1)">NavLink
to</span>="/faq"<span style="color: rgba(0, 0, 0, 1)">
activeStyle</span>=<span style="color: rgba(0, 0, 0, 1)">{{
    fontWeight: </span>'bold'<span style="color: rgba(0, 0, 0, 1)">,
    color: </span>'red'<span style="color: rgba(0, 0, 0, 1)">
   }}
</span>&gt;FAQs&lt;/NavLink&gt;

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当event id为奇数的时候,激活链接</span>
const oddEvent = (match, location) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">match) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
}
const eventID </span>=<span style="color: rgba(0, 0, 0, 1)"> parseInt(match.params.eventID)
</span><span style="color: rgba(0, 0, 255, 1)">return</span> !isNaN(eventID) &amp;&amp; eventID % 2 === 1<span style="color: rgba(0, 0, 0, 1)">
}

</span>&lt;<span style="color: rgba(0, 0, 0, 1)">NavLink
to</span>="/events/123"<span style="color: rgba(0, 0, 0, 1)">
isActive</span>=<span style="color: rgba(0, 0, 0, 1)">{oddEvent}
</span>&gt;Event 123&lt;/NavLink&gt;</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/dianzan/p/10968463.html
頁: [1]
查看完整版本: react 中的路由 Link 和Route和NavLink