鍍金池/ 問答/HTML/ Router的問題Cannot read property 'location'

Router的問題Cannot read property 'location' of undefined

項目的地址-->路由

main.js

import About from './Router/About'
import Repos from './Router/Repos'
import App from './Router/App'
import React from 'react'
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';



import { Router, Route, hashHistory } from 'react-router'

ReactDOM.render((
    <Router history={hashHistory}>
        <Route path="/" component={App}/>
        {/* add the routes here */}
        <Route path="/repos" component={Repos}/>
        <Route path="/about" component={About}/>
    </Router>
), document.getElementById('app'))

圖片描述

回答
編輯回答
吢涼

react-router@v4.0:

  • React Router被拆分成三個包:react-router,react-router-domreact-router-native,目前網(wǎng)站搭建只需要引入react-router-dom即可;

  • 路由器組件無法接受兩個及以上的子元素;

  • ...

更多改變可以查看官方文檔

正確寫法如下:

    import React from 'react'
    import ReactDOM from 'react-dom';
    import { HashRouter, Route, hashHistory, Switch } from 'react-router-dom'
    
    import About from './Router/About'
    import Repos from './Router/Repos'
    import App from './Router/App'
    
    const SliderComponent = () => (
      <Switch>
        <Route exact path='/' component={App} />
        <Route path="/repos" component={Repos}/>
        <Route path="/about" component={About}/>
      </Switch>
    )
    
    ReactDOM.render((
      <HashRouter history={hashHistory}>
        <SliderComponent />
      </HashRouter>
    ), document.getElementById('app'));
2018年9月9日 05:03
編輯回答
別逞強

現(xiàn)在直接用react-router-dom就行了,參考官方例子

2017年8月26日 20:52
編輯回答
來守候

看你 package.json 里react-router 已經(jīng)4.x了,優(yōu)先去react-router官方文檔看看各種例子~
阮一峰這文章里也寫明了

clipboard.png

2018年1月4日 05:48
編輯回答
情殺

還是報錯呀
'react-router-dom' does not contain an export named 'hashHistory'.

2018年7月25日 12:17