我知道我不是唯一一个面对这个问题的人,但是在网上搜索了一会儿并尝试了它给我的一切之后,我得在这里问我的情况。
当我尝试访问我的应用程序的路线时,当我没有使用与应用相关的方式时,我收到了流行的错误,我收到了404。例如,如果我在“/ map”时刷新页面,则会显示“无法获取/映射”。
这是我目前的配置,虽然我尝试了几个不同的devServer:{}设置并使用publicPath等进行配置:
webpack.config.js:
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
publicPath: "dist"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
}
]
},
devServer: {
historyApiFallback: {
index: "dist/index.html"
}
}
};
App.js
import React, { Component } from "react";
import { connect } from "react-redux";
import { BrowserRouter, Route } from "react-router-dom";
import Homescreen from "./Homescreen";
import Map from "./Map";
import { withStyles } from "material-ui/styles";
import Reboot from "material-ui/Reboot";
import AppBar from "material-ui/AppBar";
import Toolbar from "material-ui/Toolbar";
import Typography from "material-ui/Typography";
import Button from "material-ui/Button";
import IconButton from "material-ui/IconButton";
import MenuIcon from "material-ui-icons/Menu";
const styles = {
flex: {
flex: 1
},
menuButton: {
marginLeft: -3,
marginRight: 20
}
};
class App extends Component {
render() {
const { classes } = this.props;
return (
<BrowserRouter>
<div>
<Reboot />
<AppBar position="sticky">
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="title" color="inherit" className={classes.flex}>
WeltRaum 31
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
<Route exact path="/" component={Homescreen} />
<Route path="/map" component={Map} />
</div>
</BrowserRouter>
);
}
};
export default withStyles(styles)(App);
答案 0 :(得分:0)
您是否尝试使用HtmlWebpackPlugin?
请先将公开路径从dist
更改为/
,然后在devServer
中将historyApiFallback
更改为true
。最后安装并需要html-webpack-plugin
,然后将此插件添加到config
plugins: [
new HtmlWebpackPlugin({
template: 'dist/index.html'
})
]
答案 1 :(得分:0)
答案 2 :(得分:0)
@SVARTBERG以下链接说明了使用Webpack配置设置-> devServer的基本原理:{historyApiFallback:true}
https://tylermcginnis.com/react-router-cannot-get-url-refresh/
您必须在输出对象的“ /”中添加“ publicpath”属性值,然后在Webpack配置文件中添加如下所示的“ devserver”属性。
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index_bundle.js',
publicPath: '/'
},
devServer: {
historyApiFallback: true,
},