React router V4中的默认路由

React router v4中去掉了IndexRoute,那么如何设置默认路由呢~


class MainComponent extends Component{

    render(){

        return <div id="stage">

                <Switch>

                    <Route exact path='/' component={IndexComponent}/>

                    <Route path='/user' component={UserComponent}/>

               

                </Switch>

        </div>

    }

}

网上有这样写的:


class MainComponent extends Component{

    render(){

        return <div id="stage">

                <div><IndexComponent></div>

                <Switch>

                    <Route exact path='/' component={IndexComponent}/>

                    <Route path='/user' component={UserComponent}/>

               

                </Switch>

        </div>

    }

}

我这样写的时候会出现渲染别个页面组件的时候都会把IndexComponnet这个默认页面组件渲染了出来,麻烦解答一下。


慕森卡
浏览 726回答 1
1回答

ABOUTYOU

let Routes = (<Router>&nbsp; <div>&nbsp; &nbsp; <Link activeClassName="active" exact={true} to={{pathname: '/index'}}>首页</Link> &nbsp; &nbsp; <Link activeClassName="active" to={{pathname: '/error',search:'?msg=错误页面'}}>错误页面</Link>&nbsp; &nbsp; <div className="route-content">&nbsp; &nbsp; &nbsp; <Switch>&nbsp; &nbsp; &nbsp; &nbsp; <Route path="/index" exact component={HomePage}/>&nbsp; &nbsp; &nbsp; &nbsp; <Route path="/error" component={errorPage}/>&nbsp; &nbsp; &nbsp; &nbsp; <Redirect path="/" to={{pathname: '/index'}} />&nbsp; &nbsp; &nbsp; </Switch>&nbsp; &nbsp; </div>&nbsp; </div></Router>)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript