梁炎 發表於 2020-3-11 14:24:00

react-router-dom v5.1.2 版本 实现嵌套路由

<p>找了好久都没有找到最新版的中文文档 v5.1.2。。。</p>
<p>入口文件 src/index.js</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'./index.css'<span style="color: rgba(0, 0, 0, 1)">;
import AppRoute from </span>'./AppRoute'<span style="color: rgba(0, 0, 0, 1)">;
import </span>* as serviceWorker from './serviceWorker'<span style="color: rgba(0, 0, 0, 1)">;

ReactDOM.render(</span>&lt;AppRoute /&gt;, document.getElementById('root'));
<span style="color: rgba(0, 0, 0, 1)">
serviceWorker.unregister();</span></pre>
</div>
<p>路由表 src/AppRoute.js</p>
<div class="cnblogs_code">
<pre>import React,{Component} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import {
    HashRouter,
    BrowserRouter as Router,
    Switch,
    Route,
    Link
} from </span>"react-router-dom"<span style="color: rgba(0, 0, 0, 1)">;
import App from </span>'./page/App'<span style="color: rgba(0, 0, 0, 1)">;
import Test from </span>'./page/Test'<span style="color: rgba(0, 0, 0, 1)">;
import NotFound from </span>'./page/NotFound'<span style="color: rgba(0, 0, 0, 1)">;

class AppRoute extends Component{
    constructor(props){
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {};
    }
    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;HashRouter&gt;
      &lt;Switch&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)"> 首页 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
            </span>&lt;Route path="/" component={()=&gt;<span style="color: rgba(0, 0, 0, 1)">(
                </span>&lt;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)"> 测试 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
                  </span>&lt;Route exact path="/test" component={Test}/&gt;
                  {<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 404页面 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
                  </span>&lt;Route path="*" component={NotFound}/&gt;
                &lt;/App&gt;
            )}&gt;
            &lt;/Route&gt;
      &lt;/Switch&gt;
    &lt;/HashRouter&gt;
<span style="color: rgba(0, 0, 0, 1)">    }
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> AppRoute;</pre>
</div>
<p>首页 src/page/App/index.js</p>
<div class="cnblogs_code">
<pre>import React,{Component} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import LeftMenu from </span>'./components/LeftMenu'<span style="color: rgba(0, 0, 0, 1)">;

import </span>'./index.scss'<span style="color: rgba(0, 0, 0, 1)">;

class App extends Component{
    constructor(props){
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {};
    }
    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;div className="body"&gt;
            &lt;div className="children-container"&gt;<span style="color: rgba(0, 0, 0, 1)">
                {</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.props.children}
            </span>&lt;/div&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    }
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> App;</pre>
</div>
<p>子页面 Test(名字随意)src/page/Test/index.js</p>
<div class="cnblogs_code">
<pre>import React,{Component} from 'react'<span style="color: rgba(0, 0, 0, 1)">;

import </span>'./index.scss'<span style="color: rgba(0, 0, 0, 1)">;

class Test extends Component{
    constructor(props){
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {};
    }
    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;div className="container"&gt;<span style="color: rgba(0, 0, 0, 1)">
      测试
      </span>&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    }
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Test;</pre>
</div>
<p>使用参考链接的代码,出现了错误,提示router需要传入字符串或函数,修改为本文中的代码即可实现嵌套路由 </p>
<p>参考:在React中使用react-router-dom路由 https://www.jianshu.com/p/8954e9fb0c7e</p>

</div>
<div id="MySignature" role="contentinfo">
      
<div>
    博主 :夏秋初<br />
    地址 :https://www.cnblogs.com/xiaqiuchu/p/12462196.html <br /> <br />

    如果对你有帮助,可以点一下
    <span style="color:#ff5722;cursor: pointer;" onclick="(()=>{$('#green_channel_follow').click()})() "> 推荐 </span>
    或者
    <span style="color:#ff5722;cursor: pointer;"onclick="(()=>{$('#div_digg .diggit').click()})() "> 关注 </span>
    吗?会让我的分享变得更有动力~<br />
    转载时请带上原文链接,谢谢。<br />

</div><br><br>
来源:https://www.cnblogs.com/xiaqiuchu/p/12462196.html
頁: [1]
查看完整版本: react-router-dom v5.1.2 版本 实现嵌套路由