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

西阿瓦什

react-router通过在命令提示符窗口中运行以下代码段将dt安装在我的项目上:

npm安装react-router

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

基于教程,我将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

我知道编译器找不到路由类,并且我已经在Google和这个社区上搜索了诸如此类的问题,但是实际上我的搜索结果没有帮助。感谢您的回应。

赫马德里·达萨里(Hemadri Dasari)

您需要先导入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#路由器

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章