gpt4 book ai didi

javascript - React路由器打不开404未找到页面

转载 作者:行者123 更新时间:2023-12-01 01:21:24 25 4
gpt4 key购买 nike

我用 react 路由器创建了以下内容。

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; 

class Main extends Component {

render() {
return (
<Router>
<Switch>
<Route exact path='/' component={Content} />
<Route path='/user/:id' component={User} />
<Route path='*' component={NotFound} />
</Switch>
</Router>
);
}

export default Main

如果有任何无效网址,我想打开“未找到”页面。

我尝试打开 localhost:3000/err 但是,它不会转到 NotFound 组件。

我的 notfound 组件很简单。

import React from 'react';

const NotFound = () => (
<h2>404 Page Not Found</h2>
);

export default NotFound;

我正在使用“react-router-dom”:“^4.3.1

最佳答案

最后,我通过以下解决方案解决了。

在我的 webpack 配置文件 ( webpack.config.js ) 中我添加了以下内容:

output: {
publicPath: '/'
},

devServer: {
historyApiFallback: true
}

关于javascript - React路由器打不开404未找到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54199942/

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