【发布时间】:2017-12-16 13:50:34
【问题描述】:
尝试设置 React Router V4 并且路径总是返回 404
我有一个基本的设置
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
ReactDOM.render(
<App/>,
document.getElementById('app')
);
App.jsx
import { BrowserRouter as Router, Route} from 'react-router-dom';
import Content from './Content';
import Contact from './Contact';
render () {
return (
<div>
<Router>
<div>
<Route path='/' component={Content} />
<Route path='/contact' component={Contact} />
</div>
</Router>
</div>
)
}
both components are basic react components
Content.jsx / Contact is the same just different class name of Contact
import "../css/content.css";
import React from 'react';
export default class Content extends React.Component {
render(){
return (
<div id="content">
<div className="inner">
<div className="bgWrap">
<h2>Welcome</h2>
<p>Hey</p>
</div>
</div>
</div>
);
}
}
Content 组件在 http://localhost:8080 上工作,但我在尝试 /contact 后得到 404,即联系人
在此先感谢
【问题讨论】:
-
您的服务器在
/contact路径上浏览时是否返回客户端代码? -
我不这么认为,看网络我只是得到
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Error</title> </head> <body> <pre>Cannot GET /contact</pre> </body> </html>的回复 -
您需要安排服务器,以便所有“客户端处理”的路由都返回相同的内容。
-
好的,非常感谢,我会调查一下,您是否有任何方便的链接可以为我指明正确的方向,这是我需要在 webpack 配置中做的事情吗?
-
您是对的,非常感谢您使用此链接提供的帮助我能够正确设置 webpack-dev-server 以回退到 index.html for 404s stackoverflow.com/questions/31945763/…,可能需要其他东西生产,但我理解这个问题