嵌套的 React 路由器 4 路由不适用于 Webpack 3
Nested React router 4 routes not working on Webpack 3
正如标题所暗示的那样,我无法获得像
这样的路线
<Route path="/events/:id" component={EventDetailRoute} />
工作,正如我读到的那样 index.html 中的包似乎必须是绝对的,但是我 使用 HtmlWebpackPlugin 所以包作为相对路径注入。
我试过如下设置 webpack 的输出配置:
output: {
path: path.resolve('dist'),
filename: 'index_bundle.js',
publicPath: '/'
},
但这也不管用。
如果我尝试这条路线:http://localhost:8080/events/7, I'm getting a 404 error when trying to find http://localhost:8080/events/index_bundle.js
这是我的webpack.config:
const path = require('path'),
HtmlWebpackPlugin = require('html-webpack-plugin'),
webpack = require('webpack');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
module.exports = {
entry: './src/index.js',
output: {
path: "/" + path.resolve('dist', 'build'),
filename: 'index_bundle.js',
publicPath: '/'
},
devServer: {
historyApiFallback: true,
hot: true,
publicPath: '/'
},
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: true,
camelCase: 'dashes',
localIdentName: '[name]__[local]'
}
},
{
loader: 'resolve-url-loader'
},
{
loader: 'sass-loader'
}
]
},
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
]
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: {
loader: 'file-loader?name=[name].[ext]&outputPath=fonts/',
}
},
{
test: /\.(png|jpg)$/,
use: {
loader: 'file-loader?name=[name].[ext]&outputPath=assets/',
}
}
]
},
plugins: [
HtmlWebpackPluginConfig,
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
]
}
我正在使用 webpack 3.1.0、webpack-dev-server 2.5.1 和 react-router-dom 4.1.1
将 <base href="/" />
添加到 index.html 文件的 head 标签中
<head>
<meta charset="utf-8">
<title>React App Setup</title>
<base href="/" />
</head>
正如标题所暗示的那样,我无法获得像
这样的路线<Route path="/events/:id" component={EventDetailRoute} />
工作,正如我读到的那样 index.html 中的包似乎必须是绝对的,但是我 使用 HtmlWebpackPlugin 所以包作为相对路径注入。
我试过如下设置 webpack 的输出配置:
output: {
path: path.resolve('dist'),
filename: 'index_bundle.js',
publicPath: '/'
},
但这也不管用。
如果我尝试这条路线:http://localhost:8080/events/7, I'm getting a 404 error when trying to find http://localhost:8080/events/index_bundle.js
这是我的webpack.config:
const path = require('path'),
HtmlWebpackPlugin = require('html-webpack-plugin'),
webpack = require('webpack');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
module.exports = {
entry: './src/index.js',
output: {
path: "/" + path.resolve('dist', 'build'),
filename: 'index_bundle.js',
publicPath: '/'
},
devServer: {
historyApiFallback: true,
hot: true,
publicPath: '/'
},
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: true,
camelCase: 'dashes',
localIdentName: '[name]__[local]'
}
},
{
loader: 'resolve-url-loader'
},
{
loader: 'sass-loader'
}
]
},
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
]
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: {
loader: 'file-loader?name=[name].[ext]&outputPath=fonts/',
}
},
{
test: /\.(png|jpg)$/,
use: {
loader: 'file-loader?name=[name].[ext]&outputPath=assets/',
}
}
]
},
plugins: [
HtmlWebpackPluginConfig,
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
]
}
我正在使用 webpack 3.1.0、webpack-dev-server 2.5.1 和 react-router-dom 4.1.1
将 <base href="/" />
添加到 index.html 文件的 head 标签中
<head>
<meta charset="utf-8">
<title>React App Setup</title>
<base href="/" />
</head>