作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设以下路由设置:
ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/">
<Route component={Layout} onEnter={checkIndexAuthorization(store)}>
<IndexRoute component={Home} />
<Route path="/home" component={Home} />
</Route>
<Route component={AuthLayout} onEnter={checkLoginAuthorization(store)}>
<Route path="/login" component={Login} />
</Route>
</Route>
</Router>
</Provider>,
document.getElementById('root'),
)
Layout
和登录页面
AuthLayout
.正常布局用于应用程序本身。
Layout
组件文件:
//========================================
// IMPORTS
//========================================
import React, { PropTypes } from 'react';
//========================================
// COMPONENT
//========================================
const Layout = props => (
<main className="app">
<header>
{props.sectionTitle}
</header>
<section>
{props.children}
</section>
<footer>
</footer>
</main>
)
Layout.propTypes = {
children: PropTypes.node,
sectionTitle: PropTypes.string // <-- not being set anywhere currently
}
//========================================
// EXPORTS
//========================================
export default Layout;
最佳答案
我也只是在看这个 - 在我的应用程序中,我有私有(private)路由,只有在身份验证后才可用,我看了大约 5 分钟后才想起!
如果您在应用程序中创建私有(private)路由,请不要忘记将 props 传递给新创建的路由组件,例如:
<PrivateRoute path="/path/to/route" name={'Route name'} component={RouteComponent} layout={SimpleLayout} />
const PrivateRoute = ({ layout: Layout, component: Component, ...rest }) => (
<Route {...rest} render={props => {
const newComponent = Layout ?
<Layout><Component {...rest} {...props} /></Layout>
:
<Component {...props} />;
return Auth.isUserAuthenticated() ? (
newComponent
) : (
<Redirect to={{pathname: '/login', state: { from: props.location }}} />
)
}}/>
);
this.props.children.props.name
关于reactjs - 如何将 Prop 传递给 react-router 中使用的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46179774/
我是一名优秀的程序员,十分优秀!