我已经保护了/control-panel页面,但当我转到/control-panel页面404时,页面首先呈现,然后加载页面,你知道如何修复吗?
代码:
<Switch>
<Route exact path='/' component={HomePage} />
<Route exact path='/help' component={FAQ} />
{admin ? <PrivateRoute authed={userSignedIn} path="/control-panel" exact component={ControlPanel} /> : null}
<Route component={NoMatch} />
</Switch>更新为:
{!my_protected_urls.includes(window.location.pathname) && <Route component={NoMatch} />}发布于 2019-06-28 07:05:31
这是因为在执行此代码之前,admin的值不为真。
正如我在我的评论中指出的,这取决于你如何获得admin的价值
然而,对于这种情况,一种更容易的解决方法是使用pathname。
试试这个:
<Switch>
<Route exact path='/' component={HomePage} />
<Route exact path='/help' component={FAQ} />
{admin ? <PrivateRoute authed={userSignedIn} path="/control-panel" exact component={ControlPanel} /> : null}
{window.location.pathname!=='/control-panel' && <Route component={NoMatch} />}
</Switch>这应该可以解决您的问题。干杯!!
发布于 2019-06-28 05:44:44
确保在路由器初始化之前设置了'admin‘值,然后尝试此操作。
<Switch>
<Route exact path='/' component={HomePage} />
<Route exact path='/help' component={FAQ} />
{admin ? <PrivateRoute authed={userSignedIn} path="/control-panel" exact component={ControlPanel} /> : null}
<Route component={NoMatch} />
</Switch>https://stackoverflow.com/questions/56798650
复制相似问题