当前位置 : 主页 > 网页制作 > React >

reactjs – React-JS / react-router-dom中的路由问题

来源:互联网 收集:自由互联 发布时间:2021-06-15
我通过在命令提示符窗口中运行以下代码片段在我的项目上安装了react-router: npm install react-router 我发现react-router安装成功,因为安装时没有错误,输出也是: react-router@4.3.1 updated 1 packa
我通过在命令提示符窗口中运行以下代码片段在我的项目上安装了react-router:

npm install react-router

我发现react-router安装成功,因为安装时没有错误,输出也是:

  • react-router@4.3.1
    updated 1 package in 19.53s

基于tutorial,我设置了我的Index.js,如下面的代码:

ReactDOM.render((
   <Router history = {browserHistory}>
      <Route path = "/" component = {App}>
         <IndexRoute component = {Home} />
         <Route path = "home" component = {Home} />
         <Route path = "about" component = {About} />
         <Route path = "contact" component = {Contact} />
      </Route>
   </Router>
), document.getElementById('app'))

现在,当我想编译/构建我的应用程序时,会发生以下错误:

./src/index.js
  Line 14:  'Router' is not defined          react/jsx-no-undef
  Line 14:  'browserHistory' is not defined  no-undef
  Line 15:  'Route' is not defined           react/jsx-no-undef
  Line 16:  'IndexRoute' is not defined      react/jsx-no-undef
  Line 17:  'Route' is not defined           react/jsx-no-undef
  Line 18:  'Route' is not defined           react/jsx-no-undef
  Line 19:  'Route' is not defined           react/jsx-no-undef

我知道编译器找不到路由类,我搜索了谷歌和这个社区的问题,但实际上我的搜索结果没有帮助.谢谢你的回复.

您需要在调用它们之前导入Router,Route和index.js文件

import { Router, Route } from "react-router";

React-Router v4不支持IndexRoute,而是可以使用Route精确代替IndexRoute

<Route exact path={path} component={Component} />

编辑:

react-router v4不支持browserHistory,因此这里的检查有替代解决方案https://github.com/ReactTraining/react-router/blob/25776d4dc89b8fb2f575884749766355992116b5/packages/react-router/docs/guides/migrating.md#the-router

网友评论