FAQ
'Switch' is not exported from 'react-router-dom'
原因如下: 在 react-router-dom 版本v5中是可以这样去写的,而我用的是版本 v6的, 它用 Routes 组件替换了 Switch。
- 旧版
// 使用 Switch
import {Route, Switch} from 'react-router-dom'
return(
<Switch>
<Route path="/about" component={About}/>
<Route path="/home" component={Home}/>
</Switch>
)
- 新版
// 使用 Routes
import {Route, Routes} from 'react-router-dom'
return(
<Routes>
<Route path="/about" element={<About/>}/>
<Route path="/home" element={<Home/>}/>
</Routes>
)
Routes 类似于 组件 Switch,但功能更强大,优点在于,Routes 根据最佳匹配选择路线,而不是按照顺序遍历,这就避免了由于无法访问的路由而导致的错误。
参考
react 导入 react-router-dom引入Switch报 ‘Switch‘ is not exported from ‘react-router-dom‘