【发布时间】:2018-12-27 19:59:27
【问题描述】:
const Main = () => (
<div>
<Header />
<StyledMain>
<Header />
<Switch>
<Route exact path="/" component={Splash} render={()=>{alert('dd"')}}/>
<Route path="/files" component={Files} />
<Route path="/archived" component={Archived} />
<Route path="/extract/:filename" component={Extract} />
<Route path="/docs/api" component={Docs} />
</Switch>
</StyledMain>
</div>
)
在我的 main.js 中,每当我进入主路由 ('/') 时,我都会尝试触发警报。但是,这不起作用。
我也试过onEnter,但发现这是针对旧版本的。
这是正确的用法吗?
【问题讨论】:
-
render属性用于在Route的path与 URL 匹配时呈现某些内容。将alert放在Splash组件构造函数中是否适合您的用例? -
我不明白。 “/”路径将用户重定向到 Spalsh。
-
是的,但是您可以将逻辑放在
Splash组件中,而不是在render属性中调用它。 -
我最终想要做的是放置一个身份验证模式,以便用户在没有身份验证的情况下无法访问路由。
-
然后您可以创建和使用 PrivateRoute 并在那里执行逻辑。