简体   繁体   English

TypeError:无法读取未定义的属性“位置”

[英]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 docsBrowserRouter 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.

相关问题 未捕获的TypeError:无法读取未定义的属性“位置” - Uncaught TypeError: Cannot read property 'location' of undefined 错误为:TypeError:无法读取未定义的属性“位置” - Getting error as :TypeError: Cannot read property 'location' of undefined TypeError:无法读取未定义的属性“位置” - TypeError: Cannot read property &#39;location&#39; of undefined 未捕获的类型错误:无法读取未定义窗口位置的属性“拆分” - Uncaught TypeError: Cannot read property 'split' of undefined window location TypeError:使用“窗口”时无法读取未定义反应错误的属性“位置” - TypeError: Cannot read property 'location' of undefined react error on using 'window' Chrome:未捕获的TypeError:无法读取未定义的属性“位置” - Chrome: Uncaught TypeError: Cannot read property 'location' of undefined TypeError:无法读取未定义的 React Framer Motion API 的属性“位置” - TypeError: Cannot read property 'location' of undefined React Framer Motion API TypeError:无法读取未定义的属性“0” - TypeError: Cannot read property '0' of undefined TypeError无法读取未定义的属性 - TypeError Cannot read property of undefined TypeError:无法读取未定义的属性… - TypeError: Cannot read property … of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM