如何在React中使用不同路径上的组件?
我试图像这样完成它,但是我收到了一个错误:
<Router>
<App>
<Route exact path='/registro' component={Registro}/>
<Route exact path='/registrar' component={Registrar}/>
<Route exact path="/home" component={Home} />
</App>
<Route exact path="/login" component={Login} />
</Router>,
document.getElementById('root'));
应用:
render() {
const { children } = this.props;
return (
<div>
{children}
</div>
);
}
答案 0 :(得分:3)
Router
应该只有一个孩子。所以你可以将你的元素包装在一个div中,如下所示:
<Router>
<div>
<App>
<Route exact path='/registro' component={Registro}/>
<Route exact path='/registrar' component={Registrar}/>
<Route exact path="/home" component={Home} />
</App>
<Route exact path="/login" component={Login} />
</div>
</Router>