尝试设置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>
);
}
}
内容组件适用于http://localhost:8080,但是一旦我尝试/联系,我就得到了404,联系人
非常感谢提前
答案 0 :(得分:0)
尝试:
App.jsx
import {Switch as RouterSwitch, Route} from 'react-router-dom';
import Content from './Content';
import Contact from './Contact';
const App=()=> (
<RouterSwitch>
<Route path='/' component={Content} />
<Route path='/contact' component={Contact} />
</RouterSwitch>
);
export default App;
希望它对你有帮助:))
答案 1 :(得分:0)
感谢Davin Tryon在评论中,为了解决我的问题,我需要配置webpack-dev-server以返回所有客户端路由到我的案例index.html,然后我将使用他的路由器来处理我的404&#39;秒。配置webpack我遵循这个deref_mut