在 react-router-dom 中从我的页面中删除“.ghtml”
remove '.ghtml' from my pages in react-router-dom
我正在使用具有此配置的 react-router-dom (4.1.1):
import React from 'react';
import { BrowserRouter as Router, Route, Switch, Redirect} from 'react-router-dom';
import App from './app/App.js';
const Wildcard = () => <Redirect to="/" />;
const Routes = () => (
<Router>
<div>
<Switch>
<Route exact path="/" component={App} />
<Route exact path="/:section/:news/:title" component={App} />
<Route exact path="/*" render={Wildcard} />
</Switch>
</div>
</Router>
);
export default Routes;
我希望这个 url、"localhost:3000/section/news/title.ghtml" 匹配第二个条件,但我得到这个:
Cannot GET /section/news/title.ghtml
当我删除 .ghtml 时它工作正常。
这是 React 路由器可以理解 .ghtml 的一种方式吗?
解决了 Vanilla Javascript 的问题:
const url = location.pathname.replace('.ghtml','');
history.replaceState(null,null,url);
虽然很简单,希望能帮助解决这个问题的人
我正在使用具有此配置的 react-router-dom (4.1.1):
import React from 'react';
import { BrowserRouter as Router, Route, Switch, Redirect} from 'react-router-dom';
import App from './app/App.js';
const Wildcard = () => <Redirect to="/" />;
const Routes = () => (
<Router>
<div>
<Switch>
<Route exact path="/" component={App} />
<Route exact path="/:section/:news/:title" component={App} />
<Route exact path="/*" render={Wildcard} />
</Switch>
</div>
</Router>
);
export default Routes;
我希望这个 url、"localhost:3000/section/news/title.ghtml" 匹配第二个条件,但我得到这个:
Cannot GET /section/news/title.ghtml
当我删除 .ghtml 时它工作正常。
这是 React 路由器可以理解 .ghtml 的一种方式吗?
解决了 Vanilla Javascript 的问题:
const url = location.pathname.replace('.ghtml','');
history.replaceState(null,null,url);
虽然很简单,希望能帮助解决这个问题的人