猿问

react-router4 map循环出的<Link>可以切换路由地址,但是切换不了组件

<ul className="nav">
    <li>
        <Link to="/section1">test1</Link>
    </li>
    <li>
        <Link to="/section2">test2</Link>
    </li>
    <li>
        <Link to="/section3">test3</Link>
    </li>
    //上边直接写正常
    ////////////////////////////////////////
    //下边就不行,地址可以切换,组件出不来,也切换不了    {list.map((item, index) => {
        return <li key={index}>            <Router>
                <Link to={`/section${index + 1}`}>{item}</Link>
            </Router>
            </li>
        })}</ul>{this.props.children}
<Switch>
    <Route path="/section1" component={Section1}/>1    <Route path="/section2" component={Section2}/>2    <Route path="/section3" component={Section3}/>3</Switch>


Cats萌萌
浏览 1705回答 1
1回答

尚方宝剑之说

{list.map((item,&nbsp;index)&nbsp;=>&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<li&nbsp;key={index}>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Link&nbsp;to={`/section${index&nbsp;+&nbsp;1}`}>{item}</Link> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li> &nbsp;&nbsp;&nbsp;&nbsp;})}因为我嵌套了2层<Router>去掉就可以了...
随时随地看视频慕课网APP
我要回答