反应路由器v ^ 4.0.0未捕获的TypeError:无法读取未定义的属性“位置” [英] react router v^4.0.0 Uncaught TypeError: Cannot read property 'location' of undefined

查看:67
本文介绍了反应路由器v ^ 4.0.0未捕获的TypeError:无法读取未定义的属性“位置”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用反应路由器时遇到了一些麻烦(我正在使用版本^ 4.0.0)。

I've been having some trouble with react router (i'm using version^4.0.0).

这是我的index.js

this is my index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import { Router, Route, Link, browserHistory } from 'react-router';


ReactDOM.render(
  <Router history={browserHistory} >
    <Route path="/" component={App} />

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

App.js就是任何东西。我在这里发布基本的,因为它不是问题(我相信)

the App.js is just anything. i'm posting the basic one here, cause it's not the problem (i believe)

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

这是我检查控制台日志时发生的事情

and this is what happens when i check the console log

Router.js:43 Uncaught TypeError: Cannot read property 'location' of undefined
    at new Router (Router.js:43)
    at ReactCompositeComponent.js:295
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)
    at ReactCompositeComponentWrapper._constructComponentWithoutOwner (ReactCompositeComponent.js:294)
    at ReactCompositeComponentWrapper._constructComponent (ReactCompositeComponent.js:280)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:188)
    at Object.mountComponent (ReactReconciler.js:46)
    at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:371)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:258)
    at Object.mountComponent (ReactReconciler.js:46)

哦,这就是包装ge.json以防万一

oh, and this is the package.json just in case

{
  "name": "teste2",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-router": "^4.0.0"
  },
  "devDependencies": {
    "react-scripts": "0.9.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

我一直在其他地方检查这个,但我找不到解决方法。

i've been checking this on other places, but i didn't find a way to solve it.

非常感谢你们的耐心和帮助!!

Thank you guys very much for your patience and help!!

推荐答案

<你正在做一些错事。首先,browserHistory不是V4中的东西,所以你可以删除它。其次,您从 react-router 导入所有内容,它应该是 react-router-dom 。第三, react-router-dom 不会导出路由器,而是导出 BrowserRouter 所以你需要从导入{code> import {BrowserRouter as Router}。看起来你只是拿了你的V3应用程序,并期望它与v4一起工作,这不是一个好主意。

You're doing a few things wrong. First, browserHistory isn't a thing in V4, so you can remove that. Second, you're importing everything from react-router, it should be react-router-dom. Third, react-router-dom doesn't export a Router, instead, it exports a BrowserRouter so you need to import { BrowserRouter as Router } from 'react-router-dom. Looks like you just took your V3 app and expected it to work with v4, which isn't a great idea.

这篇关于反应路由器v ^ 4.0.0未捕获的TypeError:无法读取未定义的属性“位置”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆