gpt4 book ai didi

javascript - 无法导航到 react-router-dom URL

转载 作者:行者123 更新时间:2023-11-29 10:31:08 27 4
gpt4 key购买 nike

SO 上有一些主题,例如 this one建议更改 Webpack 和 this one建议设置一个包罗万象的。

我正在为三个路由使用 react-router-dom;与此处的其他问题类似,/ 路径有效,但 /cars/about 均无效。

import React, {Component} from 'react';
import {render} from 'react-dom';
import {BrowserRouter, Route, Switch, Link} from 'react-router-dom';


const Home = () => (
<h1>Home</h1>
)

const Car = () => (
<h1>Cars</h1>
)

const About = () => (
<h1>About</h1>
)

render(
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/cars" component={Car}/>
<Route path="/about" component={About}/>
</Switch>
</BrowserRouter>,
document.getElementById('container')
);

我尝试将 publicPathhistoryApiFallback 添加到我的 webpack 配置中:

module.exports = {
entry: ['./src/index.jsx'],
output: {
path: path.resolve('public'),
filename: 'bundle.js',
publicPath: '/'
},
module: {
loaders: [
{test: /\.js$/,loader: 'babel-loader',exclude: /node_modules/},
{test: /\.jsx$/,loader: 'babel-loader',exclude: /node_modules/}
]
},
devServer: {
historyApiFallback: true
}
}

但是当我导航到 http://localhost:8080/cars 时,我在浏览器上收到了一条 Cannot GET/cars 消息和大量类似的错误对此:

Refused to load the font 'data:font/woff;base64,d09GRgABAAAAAGz8ABEAAAAA09gAAQABAAAAAAAAAAAAAAAAAAAAAAAAAABHREVGAAABgAAAAC8AAAA0AsQC9UdQT1MAAAGwAAATuAAANLwBEyF1R1NVQgAAFWgAAAIWAAAEZqfk0PVPUy8yAAAXgAAAAFAAAABgaNCCw2NtYXAAABfQAAABkwAAAkQk8AV7Y3Z0IAAAGWQAAABiAAAAugGiQq9mcGdtAAAZyAAABZcAAAvNb3/BHGdhc3AAAB9gAAAACAAAAAgAAAAQZ2x5ZgAAH2gAAESvAAB8yu28l3FoZWFkAABkGAAAADYAAAA2BmibVWhoZWEAAGRQAAAAIAAAACQHMQRzaG10eAAAZHAAAAJDAAAEImBmMbxsb2NhAABmtAAAAhoAAAIaflxdR21heHAAAGjQAAAAIAAAACACjgzgbmFtZQAAaPAAAACdAAABKBQEL8lwb3N0AABpkAAAAsMAAAS9pi3QFXByZ...w76a3jVVUpJzXkBsRtNQoHWTV2mt2UusrulbnIrkvAXNBDFtTVIB8Uoau4pSruq4q7qq2dHpQADUAT0IJ5ra0yPUAfMACMFY6pOtegV/9D7UtTZx72tTeXI4JdcUXh7Pb67D7I/S05AwjAAiYsNie6WOwc4MiYCORSEx+ZExuCvQpiNSRmAdL8wDs2AslUOgp8HfnSYyfCYjrE7w8QDucyS0aXjH0zGk7FX991RgON6L7Qma6pQ+SzA0Qw1x9+HgNFtrBk+F9RsmDpTShvNJL4BDWtP8IAeAFj8N7BcCIoYiMjY1/kBsadHAwcDMkFGxnYnLZFMFgwMLAyaIE4DjzeHPYs+mzKLOIsrBxQoVA2VyZzFk0mWSawELfTPmEGAQYeBk4GNpBGTqCYgNM+BgcYhIgxM7hsVGHsCIzY4NARsZE5xWWjGoi3i6OBgZHFoSM5JAKkJBIIHHh8ORxZDNlUWSRZWHm0djD+b93A0ruRicFlA1vcRtYUFwBQJimV' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.

最佳答案

通常在尝试自己解决这个问题 30 分钟后,感到沮丧并在 SO 上发帖;我在 2 分钟后...!

我添加到我的 webpack 文件中的配置毫无意义;看到我正在使用 express

提供我的文件

catch-all 方法正是我所需要的,但我做错了。你有两个选择。执行适当的“全部捕获”并将所有内容发送到 HTML 文件...

app.get('*', (req, res) => {
res.sendFile(appRootPath + '/public/index.html');
});

或者,如果像我一样,您只想发送一个特定的端点,这样您仍然可以管理“其他事情”,您可以...

app.get('/react*', (req, res) => {
res.sendFile(appRootPath + '/public/index.html');
});

具体提及,因为这就是让我失望的原因,请确保您执行 /react*/ 而不是 /react/*

然后我可以将我的路线更新为以下内容并直接浏览到它们...

render(
<BrowserRouter>
<Switch>
<Route exact path="/react" component={Home}/>
<Route exact path="/react/car" component={Car}/>
<Route path="/react/about" component={About}/>
</Switch>
</BrowserRouter>,
document.getElementById('container')
);

Heres an example where I typed in a random URL

关于javascript - 无法导航到 react-router-dom URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757819/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com