BrowserRouter解决刷新"cannot GET /UR

2019-04-30  本文已影响0人  账房先生2016
  1. 如果有服务器
import express from 'express';
const app = express();
import path from 'path';

app.use(express.static('dist'))

app.get('/*', (req, res) => {
    return res.sendFile(path.join( path.resolve(path.dirname('')), './dist', 'index.html'));
});

app.listen(3041, function () {
    console.log("Example app listening at port 3041")
});

index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>TMRPlus-您的TMR车伴侣</title>
</head>

<body>
    <div id="root"></div>
    <!--导入 Webpack 输出的 JavaScript 文件-->
    <script src="/index.js"></script> // 路径非常重要。不能为./index.js或index.js
</body>

</html>
  1. 调试中解决该问题
// 在webpack config 中加入:
devServer: {
        historyApiFallback: true,
    },
上一篇 下一篇

猜你喜欢

热点阅读