Javascript 如何重定向主页?
我的问题是,我有这样的路线,但当我尝试例如http://localhost:3000/examp1 我只想重定向到主页当我写东西的时候我怎么能做到http://localhost:3000/***ı进入页面,但没有显示如何检测以及如何重定向主页?有人帮忙吗Javascript 如何重定向主页?,javascript,html,css,reactjs,react-router-dom,Javascript,Html,Css,Reactjs,React Router Dom,我的问题是,我有这样的路线,但当我尝试例如http://localhost:3000/examp1 我只想重定向到主页当我写东西的时候我怎么能做到http://localhost:3000/***ı进入页面,但没有显示如何检测以及如何重定向主页?有人帮忙吗 <Router> <Switch> <Route path='/' component={Home} exact /> &l
<Router>
<Switch>
<Route path='/' component={Home} exact />
<Route path='/xyz' component={asf} exact />
<Route path='/asd' component={asd} exact />
<Route path='/fasd/:slug' component={asd} exact />
<Route path='/asd' component={asd} exact />
<Route path='/asd/:slug' component={asd} exact />
<Route path='/asd' component={asd} exact />
<Route path='/asd' component={fas} exact />
<Route path='/asd' component={fasd} exact />
</Switch>
</Router>
在底部添加一个
,在
标记中没有路径
属性,在没有匹配项的情况下应该有效,然后使用
标记重定向到主页
<Router>
<Switch>
<Route path='/' component={Home} exact />
<Route path='/xyz' component={asf} exact />
<Route path='/asd' component={asd} exact />
<Route path='/fasd/:slug' component={asd} exact />
<Route path='/asd' component={asd} exact />
<Route path='/asd/:slug' component={asd} exact />
<Route path='/asd' component={asd} exact />
<Route path='/asd' component={fas} exact />
<Route path='/asd' component={fasd} exact />
<Route component={() => <Redirect to="/" />} />
</Switch>
</Router>
} />
在底部添加一个
,在
标记中没有路径
属性,在没有匹配的情况下应该有效,然后使用
标记重定向到主页
<Router>
<Switch>
<Route path='/' component={Home} exact />
<Route path='/xyz' component={asf} exact />
<Route path='/asd' component={asd} exact />
<Route path='/fasd/:slug' component={asd} exact />
<Route path='/asd' component={asd} exact />
<Route path='/asd/:slug' component={asd} exact />
<Route path='/asd' component={asd} exact />
<Route path='/asd' component={fas} exact />
<Route path='/asd' component={fasd} exact />
<Route component={() => <Redirect to="/" />} />
</Switch>
</Router>
} />
在react router 1.0.0中删除了DefaultRoute和NotFoundRoute 我想强调的是,带星号的默认路由必须是当前层次结构级别中的最后一个路由才能工作。否则,它将覆盖树中出现在它后面的所有其他路由,因为它是第一个路由,并且匹配每个路径 对于react路由器1、2和3 如果要显示404并保留路径(与NotFoundRoute功能相同)
在react router 1.0.0中删除了DefaultRoute和NotFoundRoute 我想强调的是,带星号的默认路由必须是当前层次结构级别中的最后一个路由才能工作。否则,它将覆盖树中出现在它后面的所有其他路由,因为它是第一个路由,并且匹配每个路径 对于react路由器1、2和3 如果要显示404并保留路径(与NotFoundRoute功能相同)
您可以简单地使用react路由器dom库的重定向组件
<Router>
<Switch>
<Route path='/' component={Home} exact />
<Route path='/xyz' component={asf} exact />
<Route path='/asd' component={asd} exact />
<Route path='/fasd/:slug' component={asd} />
<Route path='/asd' component={asd} exact />
<Route path='/asd/:slug' component={asd} />
<Redirect to="/" />
</Switch>
</Router>
您只需使用react router dom库的重定向组件即可
<Router>
<Switch>
<Route path='/' component={Home} exact />
<Route path='/xyz' component={asf} exact />
<Route path='/asd' component={asd} exact />
<Route path='/fasd/:slug' component={asd} />
<Route path='/asd' component={asd} exact />
<Route path='/asd/:slug' component={asd} />
<Redirect to="/" />
</Switch>
</Router>
非常感谢非常感谢