[英]TypeError: Cannot read property 'location' of undefined
This is index js file. 这是索引js文件。
import React from "react";
import { render } from "react-dom";
import {Router, Route, browserHistory} from "react-router";
import {EmployeeList} from "./employeeList";
import {EditForm} from "./edit";
class App extends React. Component{
render(){
return (
<Router history={browserHistory}>
<Route path={"/"} component={EmployeeList} />
</Router>
);
}
}
render(<App />, window.document.getElementById("app"));
This is employeeList js file. 这是employeeList js文件。
import React from "react";
export class EmployeeList extends React. Component {
constructor(props) {
super(props);
};
render() {
return (
<div>
<h2>Basic Table</h2>
<table class="table table-dark table-striped">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Button</th>
</tr>
</thead>
{student}
</table>
</div>
);
}
}
Main page is index.js from that page i want call employeeList page..but its showing undefined location type error.install npm react-route too still its not working..unable to find out the error. 主页是该页面的index.js,我想调用employeeList页面。但是其显示未定义的位置类型error.install npm react-route仍然无法正常工作..无法找出错误。
You should use BrowserRouter
from 'react-router-dom'
instead of the Router
. 您应该从
'react-router-dom'
而不是Router
使用BrowserRouter
。
The Router component is intended to be used when working with a custom history, so the history
prop is required. 路由器组件旨在用于自定义历史记录,因此需要
history
道具。 To work with the react-router on the web browser, you should use the BrowserRouter from react-router-dom
. 要在Web浏览器上使用
react-router-dom
,应使用react-router-dom
的BrowserRouter。 Just for clarity, here is the Router docs , and the BrowserRouter docs 为了清楚起见,这是Router docs和BrowserRouter docs
And your code should be: 您的代码应为:
import { BrowserRouter } from 'react-router-dom';
... ...
class App extends React. Component{
render(){
return (
<BrowserRouter>
<Route path={"/"} component={EmployeeList} />
</BrowserRouter>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.