报如下错,查react-router-dom版本。 因为:react-router-dom从V5升级到V6造成的 (1) 将Switch 重命名为 Routes (2) Route 的新特性变更 ,component/render被element替代 (3)嵌套路由变得更简单
我们知道,在react项目里使用路由的集中管理,需要安装react-router-config,而在我安装引入之后就出现以上报错, 我只好顺着错误去看看react-router-config的源码,这里发现react-router-config使用的竟然还是react-router v5的Switch,我当时就无语了,哥们时代变了你不知道吗,我的react-router-dom是^6,所以才产生了报错。
针对你的问题,'switch' (imported as 'switch') was not found in 'react-router-dom',这通常意味着你尝试从react-router-dom包中导入一个不存在的组件switch。以下是分点回答,旨在帮助你解决此问题: 检查是否正确安装了react-router-dom包: 确保你的项目中已经安装了react-router-dom包。你可以通过运行以下命令...
function App() { return (<Router><Linkto="/">Home</Link><Linkto="/about">About</Link><Linkto="/users">Users</Link>{/* A<Switch>looks through its children<Route>s and renders the first one that matches the current URL. */}<Switch><Routepath="/about"><About/></Route><Routepath=...
export 'withRouter' (imported as 'withRouter') was not found in 'react-router-dom' 原因:这些报错原因均为'Switch' 和'Redirect' 是react-router 5版本的接口,而最新版本是 "react-router-dom": "^6.2.1",并且已经将Switch改为Routes。 解决办法有二: ...
import{BrowserRouterasRouter,Route,Switch}from'react-router-dom'; 1. 原因:安装了6.x版本的react-router-dom,在新版本的代码中,已经将 Switch改为Routes Redirect 改为Navigate ...
问“Switch”(导入为“Switch”)在“react路由器”错误中没有发现。EN语义是: 计算表达式的值. 将其...
以下是一个示例的React Router配置,用于处理参数不匹配时返回404页面: 代码语言:javascript 复制 import{BrowserRouterasRouter,Route,Switch}from'react-router-dom';importNotFoundfrom'./components/NotFound';constApp=()=>{return(<Router><Switch>{/* 其他路由规则 */}<Route component={NotFound}/></Switc...
<Switch> <Route exact path="/" component={App}/> <Route path="/welcome" component={Welcome}/> <Route component={NotFound}/> </Switch> </Router> ); ReactDOM.render( <Root/>, document.getElementById('root') ); 原文由metakermit发布,翻译遵循 CC BY-SA 4.0 许可协议...
export 'Switch' (imported as 'Switch') was not found in 'react-router-dom' 使用react-router时报错! 解决方案: 将react-router版本降到5即可