加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:11:58 所属栏目:百科 来源:网络整理
导读:我通过在命令提示符窗口中运行以下代码片段在我的项目上安装了react-router: npm install react-router 我发现react-router安装成功,因为安装时没有错误,输出也是: react-router@4.3.1 updated 1 package in 19.53s 基于tutorial,我设置了我的Index.js,如
我通过在命令提示符窗口中运行以下代码片段在我的项目上安装了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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读