简体   繁体   English

React-router错误直接通过路径访问或刷新页面

[英]React-router error accessing directly by the path or refreshing the page

I have implemented react-router without any problem, it's working properly, but for some reason in the case an user refresh the page or try to access directly to a different page of the main one by the path i'm getting an error like Cannot GET /whatever (eg Cannot GET /blog). 我已经实现了react-router没有任何问题,它工作正常,但由于某些原因,用户刷新页面或尝试通过路径直接访问主页的不同页面我得到一个错误,如不能GET /无论如何(例如,不能GET /博客)。

Below is the code: 以下是代码:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory } from 'react-router';
import { Provider } from 'react-redux';
import store from 'store';

// Layouts
import App from 'layouts/app';

// Components
import Portfolio     from 'ui/portfolio';
import BlogContainer from 'ui/blog-container';
import TimeLine      from 'ui/timeline';
import About         from 'ui/about'
import Contact       from 'ui/contact'

ReactDOM.render((
  <Provider store={store}>
    <Router history={browserHistory}>

        <Route component={App}>
            <Route path="/" component={Portfolio} />
            <Route path="/blog" component={BlogContainer} />
            <Route path="/timeline" component={TimeLine} />
            <Route path="/about" component={About} />
            <Route path="/contact" component={Contact} />
        </Route>

    </Router>
  </Provider>
), document.getElementById('root'));

Any idea how can I fix it. 知道如何解决它。

Note: dependencies I am using
    "react": "^0.14.3",
    "react-dom": "^0.14.3",
    "react-redux": "^4.0.6",
    "react-router": "^2.0.0",
    "redux": "^3.3.1"

The problem is that your server router (nginx, Apache...) does not know what to return and what content to deliver to your browser. 问题是您的服务器路由器(nginx,Apache ...)不知道要返回什么以及要向浏览器提供什么内容。 Basically, if your app is only a front end app that you have already bundled, admitting you're using nginx, you need this kind of config for your server: 基本上,如果你的应用程序只是你已经捆绑的前端应用程序,承认你正在使用nginx,你需要为你的服务器配置这种配置:

server {

    location / {
        try_files $uri /your/index.html; # the path to your html page
    }
} 

Then, when you will try to go directly to a route, your server will always return the html page containing your javascript bundle, and react-router should handle the rest. 然后,当您尝试直接转到路径时,您的服务器将始终返回包含您的javascript包的html页面,并且react-router应该处理其余的。

See the this explanation in the docs of react-router 请参阅react-router文档中的此解释

Remove '/' from your routes. 从您的路线中删除“/”。 Directly call component. 直接调用组件。 Eg: { 例如:{

<Route component={ App } path='/master'>
        <Route path = 'board' component={BoardView} />
        <Route  path = 'team'  component={View} />
        <Route path = 'all' component={All} />
        <Route path = 'shots' component={Shots} />
        <Route path = 'home' component={Home} />
        <Route path = 'topper' component={Toppers} />
        <Route path = 'overlay' component={Overlay} />
        <Route path = 'earn' component={Earn} />

} }

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

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