2017-05-25 59 views
1

当通过React-router的Link组件改变URL时,My Routing正常工作。但是,如果我尝试在浏览器中手动更改URL,它会遇到404错误。手动更改URL时,反应路由不起作用React-router 4

下面是routes.js文件

import React from "react"; 
import {Route, Switch, NotFoundRoute} from 'react-router-dom'; 
import App from "./components/app.jsx"; 
import HomePage from "./components/homePage.jsx"; 
import AboutPage from "./components/about/aboutPage.jsx"; 
import AuthorPage from "./components/authors/authorPage.jsx"; 
import ManageAuthorPage from "./components/authors/manageAuthorPage.jsx"; 
import NotFoundPage from "./components/notFoundPage.jsx"; 

var routes = (
    <App> 
     <Switch> 
      <Route name="home" exact path="/" component={HomePage} /> 
      <Route name="authors" exact path="/authors" component={AuthorPage} /> 
      <Route name="addAuthor" exact path="/author" component={ManageAuthorPage} /> 
      <Route name="manageAuthor" path="/author/:id" component={ManageAuthorPage} /> 
      <Route name="about" exact path="/about" component={AboutPage} /> 
      <Route component={NotFoundPage} /> 
     </Switch> 
    </App> 
); 

export default routes; 

的main.js文件,其中包含BrowserRouter

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import {BrowserRouter} from 'react-router-dom'; 
import routes from './routes.jsx'; 
import InitializeActions from './actions/initializeActions'; 

InitializeActions.initApp(); 

ReactDOM.render(<BrowserRouter>{routes}</BrowserRouter>, document.getElementById('app')); 

和包含导航栏header.jsx文件

import React from 'react'; 
import {Link} from 'react-router-dom'; 

class Header extends React.Component { 
    render() { 
     return (
      <nav className="navbar navbar-default"> 
       <div className="container-fluid"> 
        <Link to="/" className="navbar-brand"> 
         <img src="../../images/pluralsight-logo.png"/> 
        </Link> 
        <ul className="nav navbar-nav"> 
         <li><Link to="/">Home</Link></li> 
         <li><Link to="authors" ref={(comp) => { window.authorsTab=comp }}>Authors</Link></li> 
         <li><Link to="about">About</Link></li> 
        </ul> 
       </div> 
      </nav> 
     ); 
    } 
} 

export default Header; 

Gulpfile.js

"use strict"; 

var gulp = require('gulp'); 
var connect = require('gulp-connect'); //Runs a local dev server 
var open = require('gulp-open'); //Opens a URL in a web browser 
var browserify = require('browserify'); //Bundle JS 
var reactify = require('reactify'); //Transforms react JSX to JS 
var source = require('vinyl-source-stream'); //Use-conventional text streams with gulp 
var concat = require('gulp-concat'); //concatnates files 
var lint = require('gulp-eslint'); //lint our js files including jsx 
var babelify = require("babelify"); 
var browserSync = require("browser-sync"); 

var config = { 
    port: 9005, 
    devBaseUrl: 'http://localhost', 
    paths: { 
    html: './src/*.html', 
    js: './src/**/*.js*', 
    images: './src/images/*', 
    css: [ 
     'node_modules/bootstrap/dist/css/bootstrap.min.css', 
     'node_modules/bootstrap/dist/css/bootstrap-theme.min.css', 
     './src/dependencies/*.css', 
     'node_modules/toastr/build/toastr.css' 
    ], 
    dist: './dist', 
    mainJs: './src/main.jsx' 
    } 
} 

//start a local dev server 
gulp.task('connect', function() { 
    connect.server({ 
    root: ['dist'], 
    port: config.port, 
    base: config.devBaseUrl, 
    livereload: true 
    }); 
}); 

//opens the URL in browser 
gulp.task('open', ['connect'], function() { 
    gulp.src('dist/index.html') 
    .pipe(open({uri: config.devBaseUrl + ':' + config.port + '/'})); 
}); 

//get all the html files from 'src', bundle them and place inside 'dist' and reload the server 
gulp.task('html', function() { 
    gulp.src(config.paths.html) 
    .pipe(gulp.dest(config.paths.dist)) 
    .pipe(connect.reload()); 
}); 

gulp.task('js', function() { 
    browserify(config.paths.mainJs) 
    .transform(babelify, {presets: ["es2015", "react"]}) 
    .bundle() 
    .on('error', console.error.bind(console)) 
    .pipe(source('bundle.js')) 
    .pipe(gulp.dest(config.paths.dist + '/scripts')) 
    .pipe(connect.reload()); 
}); 

gulp.task('css', function() { 
    gulp.src(config.paths.css) 
    .pipe(concat('bundle.css')) 
    .pipe(gulp.dest(config.paths.dist + '/css')); 
}); 

gulp.task('images', function() { 
    gulp.src(config.paths.images) 
    .pipe(gulp.dest(config.paths.dist + '/images')) 
    .pipe(connect.reload()); 
}); 

gulp.task('lint', function() { 
    return gulp.src(config.paths.js) 
    .pipe(lint({configFile: 'eslint.config.json'})) 
    .pipe(lint.format()); 
}); 

//watch any changes in html files 
gulp.task('watch', function() { 
    gulp.watch(config.paths.html, ['html']); 
    gulp.watch(config.paths.js, ['js', 'lint']); 
    gulp.watch(config.paths.css, ['css']); 
}); 

//the default task 
gulp.task('default', ['html', 'js', 'css', 'images', 'lint', 'open', 'watch']); 

我试着找到多个来源的解决方案,但似乎每个人都像我一样遵循类似的方法! 请看看。在此先感谢:)

回答

1

当使用BrowserRouter时,您需要在您的webpack中添加historApiFallback: true

添加到您的WebPack配置

devServer: { 
    historyApiFallback: true, 
    }, 

吞气等价会是这样的:

historyApiFallback = require('connect-history-api-fallback') 


//start a local dev server 
gulp.task('connect', function() { 
    connect.server({ 
    root: ['dist'], 
    port: config.port, 
    base: config.devBaseUrl, 
    livereload: true, 
    middleware: [ historyApiFallback() ] 
    }); 
}); 

this链接查看更多细节

+0

感谢Shubham的建议,但我没有使用webpack。我正在使用Gulp + Browserify。 Browserify有没有类似的方法,或者我应该切换到webpack吗? –

+0

添加吞咽等效物 –

+0

再次感谢Shubham的答案。我尝试在我的gulpfile.js中集成上述模块,但不能将代码放在正确的位置。你可以看看上面问题中更新的gulpfile.js。 –

0

如果您正在使用webpack- DEV-服务器。设置:

devServer{ 
    //... 
    historyApiFallback: true 
} 

这将有助于在适当位置的任何回复404“的index.html” ......

如果你发布到server.make确保您的服务器配置,使其重定向到主HTML

表示:

app.get('/*', function(req, res) { 
    res.sendfile('index.html'); 
}); 

一饮而尽-CONNECT:

connect.server({ 
    root: ['dist'], 
    port: config.port, 
    base: config.devBaseUrl, 
    livereload: true, 
    fallback: 'index.html' 
}); 
+0

感谢LinJI的建议,但我没有使用webpack。我正在使用Gulp + Browserify。Browserify有没有类似的方法,或者我应该切换到webpack吗? –

+0

你用什么技术作为服务器? – LinJI

+0

如果您根本不在服务器上运行,则必须使用HashRouter而不是BrowserRouter ... – LinJI

0

这为我工作:

var historyApiFallback = require('connect-history-api-fallback'); 

    gulp.task('connect', function() { 
    connect.server({ 
     root: ['dist'], 
     port: config.port, 
     base: config.devBaseUrl, 
     livereload: true, 
     middleware: function(connect, opt) { 
     return [ 
      historyApiFallback({}) 
     ] 
     } 
    }); 
    }); 

感谢@Shubham卡特里和@LinJI所有帮助!