我知道你的意思。我也有這個問題,但通過使用瀏覽器同步和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