‘Switch‘ is not exported from ‘react-router-dom‘

当我想在 react-router-dom 导出 Switch 组件时,出现了这个一个错误:'Switch' is not exported from 'react-router-dom'Switch 不是从 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 根据最佳匹配选择路线,而不是按照顺序遍历,这就避免了由于无法访问的路由而导致的错误。

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值