首页 > 解决方案 > 使用 React Router 4 保护一组路由

问题描述

在 React Router 4 中玩弄受保护的路由,并想知道我已经能够开始工作的东西是否可能。

<Switch>
    <Route exact path="/" component={LandingPage} />
    <Route path="/sign-in" component={SignIn} />
    <Route exact path="/auth/home" component={Home} />
    <Route exact path="/auth/my-profile" component={UserProfile} />
    <ProtectedRoute path="/auth" />
</Switch>

我有这些路线,如上所示,我已经设置了一个名为的受保护路线/auth

我想要的是用它来保护任何和所有从/auth

所以在这个例子中

<Route exact path="/auth/home" component={Home} />
<Route exact path="/auth/my-profile" component={UserProfile} />

也将是受保护的路线。

说得通?

标签: javascriptreactjsreact-router-v4

解决方案


推荐阅读