开发者问题收集

如何消除错误“无法读取未定义的属性‘位置’”

2017-07-21
412

我制作了 react-router 的演示。但我收到此错误 无法读取未定义的属性“location” 这是我的代码

import React from 'react';
import { render } from 'react-dom';
import {Router,Route,Link,hashHistory} from 'react-router'
import Hello from './Hello';

const Home =()=> <div><h1>home</h1><Links/></div>;
const About =()=> <div><h1>About</h1><Links/></div>;
const Contact =()=> <div><h1>Contact</h1><Links/></div>

const Links =()=> 
  <nav>
  <Link to='/'>Home</Link>
    <Link to='/about'>About</Link>
    <Link to='/contact'>Contact</Link>
</nav>

const App = () => (
  <Router history ={hashHistory}>
    <Route path="/" component ={Home}></Route>
    <Route path="/about" component ={About}></Route>
     <Route path="/contact" component ={Contact}></Route>

  </Router>
);

render(



  <App />

  , document.getElementById('root'));

https://codesandbox.io/s/qYvvYWyAG

1个回答

查看此 https://codesandbox.io/s/82QO88mLl :

 import React from 'react';
import { render } from 'react-dom';
 import {Router,Route,Link,Switch} from 'react-router'
import {BrowserRouter} from 'react-router-dom'
 import Hello from './Hello';

const Home =()=> <div><h1>home</h1></div>;
const About =()=> <div><h1>About</h1></div>;
const Contact =()=> <div><h1>Contact</h1></div>;


const App = () => (
 <BrowserRouter>
   <Switch>
   <Route path="/" component={Home}></Route>
   <Route path="/about" component={About}></Route>
  <Route path="/contact" component={Contact}></Route>
   </Switch>
 </BrowserRouter>
  );

   render(<App />, document.getElementById('root'));

您正在使用具有不同语法的版本 4

Vlado Pandžić
2017-07-21