【发布时间】:2019-08-07 11:24:49
【问题描述】:
你能帮帮我吗!
我被 url 参数卡住了。当我输入 http://localhost:3000/#/willmatch/sdf 时,我被重定向了。太好了,这就是我想要的。但是当 http://localhost:3000/#/willmatch111/ 时,什么都没有发生。
当 url 不正确时是否可以重定向?
我尝试输入 http://localhost:3000/#/willmatch1 ,但我仍然使用 WillMatch,但如果 url 不正确,我想获取 NoMatch
import React, { Component } from 'react';
import { HashRouter as Router, Switch, Route, Link, Redirect } from 'react-router-dom';
class App extends Component {
render() {
return (
<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/willmatch">Will Match</Link></li>
<li><Link to="/will-not-match">Will Not Match</Link></li>
<li><Link to="/also/will/not/match">Also Will Not Match</Link></li>
</ul>
<Switch>
<Route path="/" exact component={Home} />
<Route exact path="/:id" component={WillMatch} />
{/* <Route component={NoMatch} /> */}
<Redirect to="/" />
</Switch>
</div>
</Router>
);
}
}
function Home() {
return <h1>Home</h1>
}
function WillMatch({ match }) {
return <h1>{match.params.id}</h1>
}
function NoMatch() {
return <h1>NoMatch}</h1>
}
export default App;
【问题讨论】:
-
不,他们不谈论 url 参数
标签: reactjs react-router