简体   繁体   English

未捕获的ReferenceError:未定义IndexRoute-如何解决此错误以在react-router中使用IndexRoute?

[英]Uncaught ReferenceError: IndexRoute is not defined - How do I resolve this error to use IndexRoute in react-router?

I have a simple set of routes using react-router: 我有一组使用react-router的简单路由:

ReactDOM.render(
  <Router history={appHistory}>
    <Route path="/" component={MainLayout}>
      <IndexRoute component={Index} />
    </Route>
  </Router>,

  document.getElementById('app')
);

However, the page load fails and I get the following error in the browser console: 但是,页面加载失败,并且在浏览器控制台中出现以下错误:

Uncaught ReferenceError: IndexRoute is not defined

How do I fix this? 我该如何解决?

IndexRoute isn't implicitly loaded from react-router. 不是从react-router隐式加载IndexRoute。 You need to import it in the same way you do Router and Route: 您需要以与路由器和路由相同的方式导入它:

import { Router, Route, IndexRoute } from 'react-router';

import {IndexRoute} from 'react-router' does not apply to react-route-4. import {IndexRoute} from 'react-router'不适用于react-route-4。 react-router-4 has changed in that it no longer has children. react-router-4已更改,因为它不再有子级。 However, with the Route component you can render anything that matches the path. 但是,使用Route组件,您可以呈现与路径匹配的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM