简体   繁体   English

react-router只进入索引路由

[英]react-router only goes to index route

I'm running into a situation in react where my application on recognizes the index route. 我遇到了一个反应的情况,我的应用程序识别索引路径。 my code in my app.js looks like this: 我的app.js中的代码如下所示:

import React from 'react'

import { render } from 'react-dom'

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

class App extends React.Component {
    render() {
        return (
          <div>
            <div>
                Hello There App
            </div>
          </div>
        );
    }
}   

class About extends React.Component {
    render() {
        return (
          <div>
            <div>
                Hello There About
            </div>
          </div>
        );
    }
}

class Error extends React.Component {
    render() {
        return (
          <div>
            <div>
                Hello There Error
            </div>
          </div>
        );
    }
}

React.render((
  <Router>
    <Route path="/" component={App}>
      <IndexRoute component={About}/>
      <Route path="*" component={Error}/>
    </Route>
  </Router>
), document.body)

There were some similar links I checked out: React Router only recognises index route 我检查了一些类似的链接: React Router只识别索引路由

and

React router only works with root path React路由器仅适用于根路径

But these didn't push me along. 但这些并没有让我感动。 I'm running an express server and I have a route there too: 我正在运行快速服务器,我也有一条路线:

app.use(ecstatic({ root: __dirname + '/public', handleError:true }));
app.get('/', function(request, response){
    response.sendfile("./public/index.html");
});

With or without the app.get("/"... I run into the same issue. It's also worth noting that I tried it this way as well: https://github.com/rackt/react-router but my routes didn't work that way either. I'm going on 4 days with this issue with endless research, hopefully one of you can point me in the right direction. 有或没有app.get("/"...我遇到了同样的问题。同样值得注意的是我也尝试过这种方式: https//github.com/rackt/react-router但我的路线也没有这样的工作方式。我将在这个问题上进行4天无休止的研究,希望你们中的一个能指出我正确的方向。

package.json: 的package.json:

"dependencies": {
    "body-parser": "~1.14.1",
    "browserify": "^10.2.6",
    "cors": "^2.7.1",
    "ecstatic": "~0.8.0",
    "express": "~4.0.0",
    "history": "^1.13.1",
    "mongoose": "~4.2.6",
    "react": "~0.13.3",
    "react-dom": "^0.14.3",
    "react-router": "^1.0.0",
    "reactify": "^1.1.1",
    "socket.io": "^1.3.7",
    "uglify-js": "^2.4.24",
    "watchify": "^3.2.3"
  },
  "license": "public domain",
  "devDependencies": {
    "babelify": "^6.1.2"
  }

Make your routes look like: 让你的路线看起来像:

<Router>
  <Route path="/" component={App}>
    <IndexRoute component={Index}/>
    <Route path="*" component={Error}/>
  </Route>
</Router>

the other part you are missing is { this.props.children } in your app component, where the nested route components should be mounted. 您缺少的另一部分是应用程序组件中的{ this.props.children } ,其中应安装嵌套路径组件。

Check out the master-detail example, where the NotFound route is used. 查看使用NotFound路由的master-detail示例。

And on your server I would redirect everything * to index.html not only / : 在您的服务器上,我将所有*重定向到index.html不仅仅是/

app.get('*', function(request, response){
    response.sendfile("./public/index.html");
});

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

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