我知道你的意思。我也有这个问题,但通过使用浏览器同步和connect-history-api-fallback
解决了它。
创建一个名为srcServer.js
文件并添加这样的事情(当然安装所有需要的依赖):
// Require Browsersync along with webpack and middleware for it
import browserSync from 'browser-sync';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import historyApiFallback from 'connect-history-api-fallback';
const webpackConfig = {
debug: true,
devtool: 'cheap-module-eval-source-map',
noInfo: true,
entry: './src/index',
target: 'web',
output: {
path: __dirname + '/dist',
publicPath: '',
filename: 'bundle.js'
},
plugins: [], //[your array of plugins],
module: {
loaders: [] //[your array of loaders],
}
}
const bundler = webpack(webpackConfig);
// Run Browsersync and use middleware for Hot Module Replacement
browserSync({
server: {
baseDir: 'src',
middleware: [
historyApiFallback(), // <-- the key to loading a route in the url directly (without navigating to it first)
webpackDevMiddleware(bundler, {
// Dev middleware can't access config, so we provide publicPath
publicPath: webpackConfig.output.publicPath,
stats: {colors: true},
noInfo: true
}),
// bundler should be the same as above
webpackHotMiddleware(bundler)
]
},
// no need to watch '*.js' here, webpack will take care of it for us,
// including full page reloads if HMR won't work
files: [
'src/*.html'
]
});
然后你就可以添加一个NPM脚本到您的package.json,可以说npm run start
:
"scripts": {
"start": "babel-node srcServer.js",
},
这应该成为你的文件与connect-history-api-fallback
这将允许你直接载入和重新载入http://localhost:3000/about
上面的代码似乎是正确的。几乎与[documentation](https://github.com/reactjs/react-router)相同。 – Chris
是的,不知道服务器端有什么问题,不知道怎么做才能允许这些路由。 – anivas
你是如何实现你的路由器?你有一个'Router.run'的地方吗? – Chris