输入错误的网址后,如何重定向到错误页面?

时间:2019-08-28 03:56:24

标签: reactjs

输入错误的网址后,如何重定向到“未找到”页面。

我有以下路线:

<Switch>
    <Route path="/about" component={About} />
    <Route path="/contact" component={Contact} />
    <Route path="/dashboard" component={Dashboard} />
    <Route path="/" component={Home} />
    <Route path="**" component={Notfound} />
</Switch>

我尝试使用“ **”作为路径,但这没有用。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

没有路径的<Route>总是匹配的。因此,您可以尝试执行以下操作:

<Switch>
  <Route path="/about" component={About} />
  <Route path="/contact" component={Contact} />
  <Route path="/dashboard" component={Dashboard} />
  <Route path="/" component={Home} />

  <Route component={Notfound}/>
</Switch>