react路由从V3升级到V4遇到的报错问题。

发布于 2022-09-06 09:28:31 字数 2603 浏览 13 评论 0

import { HashRouter, Route, Link, hashHistory, IndexRoute } from 'react-router-dom';
ReactDOM.render(
    <HashRouter history={hashHistory}>

        <Route path="/" component={BasePage}>
            <IndexRoute component={Login} />
            <Route path="/login" component={Login}/>
        </Route>

        <Route path="/" component={Base}>

            {/* Default route*/}
            <Route path="/singleview" component={SingleView}/>
            <Route path="/index" component={Index}/>
            <Route path="/account" component={Account}/>
            <Route path="/role" component={Role}/>
            <Route path="/search" component={Search}/>
            <Route path="/search/info" component={SearchUserInfo}/>
            <Route path="/search/device" component={SearchUserInfoDevice}/>
            <Route path="/organization" component={Organization}/>
            <Route path="/configfile" component={ConfigFile}/>
            <Route path="/submenu" component={SubMenu}/>
            <Route path="/app-push" component={AppPush}/>


        </Route>

        {/* Not found handler */}
        {/*<Route path="*" component={NotFound}/>*/}

    </HashRouter>,
    document.getElementById('app')
);

从开始的router3版本升级到4版本,这样的配置是错误的,控制台报错如下:

Uncaught Error: A <Router> may have only one child element
at invariant (eval at <anonymous> (app.js:758), <anonymous>:43:15)
at Router.componentWillMount (eval at <anonymous> (app.js:836), <anonymous>:83:29)
at eval (eval at <anonymous> (vendor171515.js:1493), <anonymous>:350:23)
at measureLifeCyclePerf (eval at <anonymous> (vendor171515.js:1493), <anonymous>:77:12)
at ReactCompositeComponentWrapper.performInitialMount (eval at <anonymous> (vendor171515.js:1493), <anonymous>:349:9)
at ReactCompositeComponentWrapper.mountComponent (eval at <anonymous> (vendor171515.js:1493), <anonymous>:260:21)
at Object.mountComponent (eval at <anonymous> (vendor171515.js:1433), <anonymous>:50:35)
at ReactCompositeComponentWrapper.performInitialMount (eval at <anonymous> (vendor171515.js:1493), <anonymous>:373:34)
at ReactCompositeComponentWrapper.mountComponent (eval at <anonymous> (vendor171515.js:1493), <anonymous>:260:21)
at Object.mountComponent (eval at <anonymous> (vendor171515.js:1433), <anonymous>:50:35)

不知道该如何配置。
采用HashRouter做路由

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天邊彩虹 2022-09-13 09:28:31
import {HashRouter ,Route,Switch,Redirect} from 'react-router-dom'
import App from '../containers/App'
import Home from '../containers/Home'
import NotFound from '../containers/404'
   <HashRouter>
                     <Switch>
                        <Route exact  path="/" component={App}/>  
                        <Redirect from="/home" to="/"/>                              
                        <Route  component={NotFound}/>
                    </Switch>                         
            </HashRouter >   
            

app.jsx

    render(){
        return(
            <div style={{height:"100%"}}>
            {
                this.state.initDone
                ?<Route path="/"  component={Home}/>                                                     
                :<div>加载中</div>
            } 
            </div>
        )
    }
枕梦 2022-09-13 09:28:31

谢邀!
首先恭喜您已解决该问题。我就大致说一下4.0几个注意的地方吧!

clipboard.png
HashRouter 不支持 location.key 和 location.state。另外由于该技术只是用来支持旧版浏览器,因此更推荐大家使用 BrowserRouter。

clipboard.png
只渲染出第一个与当前访问地址匹配的 <Route> 或 <Redirect>。4.0 机制里不默认匹配第一个符合要求的,为什么?这种设计允许我们将多个 <Route> 组合到应用程序中,例如侧边栏(sidebars),面包屑 等等。

clipboard.png
<Route> 是 4.0 中最重要的组件了。它最基本的职责就是当页面的访问地址与 Route 上的 path 匹配时,就渲染出对应的 UI 界面。

具体配置请参考官网:https://reacttraining.com/rea...

心的憧憬 2022-09-13 09:28:31

V4中没有IndexRouter了,需要Redirect代替,如: <Route exact path={'/index'} render={() => <Redirect exact to={'/'} />}/>
<HashRouter history={hashHistory}>内只能有一个子元素,你可以把内部Router用<Swtich>包裹起来。如:

<HashRouter history={hashHistory}>
    <Switch>
        <Route />
        <Route />
        <Route />
    </Switch>
</HashRouter>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文