React 使用Route报错:A <Route> is only ever to be used as the child of <Routes> element, never rendered directl

使用Route报错:A <Route> is only ever to be used as the child of <Routes> element, never rendered directl

 

 

报错原因是react-router-dom依赖为6版本,需要使用6版本的写法

 

function router(){
return (
<Routes>
    <Route path="/home" element={<Home/>} />
    <Route path="/about" element={<About/>} />
</Routes>);
}

export default router;

 

 

 

 

 或者直接安装6以下版本依赖

 

npm i react-router-dom@5.2.0

 

对比5版本写法

 

function router(){
return (
<Router>
    <Route path="/home" component={Home} />
    <Route path="/about" component={About} />
</Router>);
}

export default router;

 

posted @ 2022-09-09 17:02  秃头的铲屎官  Views(462)  Comments(0Edit  收藏  举报