【问题标题】:Nested routes inside react component反应组件内的嵌套路由
【发布时间】:2018-09-10 17:27:38
【问题描述】:

我正在寻找这个,但我找不到正确的答案。是否可以在 React Component 中像这样使用 React Router;

import { Route, Switch } from 'react-router'    
import LandingLayout from '../../layouts/landing/LandingLayout'
import AppLayout from '../../layouts/app/AppLayout'

<Switch>
  <LandingLayout>
    <Route path="/" exact="true" component={Home} />
    <Route path='/login' component={Login} />
    <Route path='/signup' component={Signup} />
  </LandingLayout>
  <AppLayout>
    <Route path='/dashboard' component={DashboardPage} />
    <Route path='/users' component={UserList} />
    <Route path='/u/:username' component={AccountPage} />
  </AppLayout>
  <Route component={NotFound} />
</Switch>

【问题讨论】:

  • 你有什么错误吗?
  • 首先,在“/dashboard”或“/users”路由页面自带Landing Layout,必须将App Layout和中间的content section渲染为空。但在“/”主页或登录页面上,它会正确呈现。
  • server.js 也记录了这一点。警告:失败的道具类型:提供给Route 的string 类型的无效道具exact,应为boolean。在 Route 中 Routes 中 Router 中 StaticRouter 中 Provider 中 LocaleProvider

标签: reactjs react-router-v4 react-component


【解决方案1】:

经过大量研究,我得到了适合我的案例的正确答案。

首先,我的 React 应用程序是一个服务器渲染的应用程序。 第二;由于性能相关问题,我正在使用 React Router Switch。

由于我的应用架构,上述解决方案对我不起作用,我遇到了一些错误,例如:

你不应该在同一个路由中使用 Route 组件和 Route 子组件;路由子节点将被忽略

所以在这里;我想使用具有多种布局的 React Router Switch。下面是如何做到这一点。

首先,您将创建一个结合了布局和组件的自定义路由器组件。说“AppRouter”

const AppRouter = ({ component: Component, layout: Layout, ...rest }) => (
   <Route {...rest} render={props => (
     <Layout>
       <Component {...props} />
     </Layout>
   )} />
)

第二;对于公共和私人路线,必须有两个不同的布局包装器

const LandingRouteLayout = props => (
  <div>
    <LandingLayout {...props}/>
  </div>
)

const AppRouteLayout = props => (
 <div>
   <AppLayout {...props}/>
 </div>
)

最后;路线

const Routes = () => {
  return (
    <Switch>
      <AppRoute exact path="/" layout={LandingRouteLayout} component={Home} />
      <AppRoute path="/login" layout={LandingRouteLayout} component={Login} />
      <AppRoute path="/signup" layout={LandingRouteLayout} component={Signup} />
      <AppRoute path="/t/:token" layout={AppRouteLayout} component={SetToken} />
      <AppRoute path='/dashboard' layout={AppRouteLayout} component={DashboardPage} />
      <AppRoute path="/u/:username" layout={AppRouteLayout} component={AccountPage} />
      <Route component={NotFound} />
    </Switch>
  )
}

【讨论】:

    【解决方案2】:

    Switch 仅适用于 Route,并且由于您在没有 Route 的情况下渲染 LandingLayout 和 AppLayout,它们都将默认渲染,虽然可以将 Routes 添加为子 Routes,但如果您将它们添加到组件中会更好,因为您想要要让 LandingLayout 和 AppLayout 分别呈现,您必须将它们编写为路由

    import { Route, Switch } from 'react-router'    
    import LandingLayout from '../../layouts/landing/LandingLayout'
    import AppLayout from '../../layouts/app/AppLayout'
    
    <Switch>
      <Route path="/landing" component={LandingLayout}/> 
      <Route path="/app" component={AppLayout} />
      <Route component={NotFound} />
    </Switch>
    

    着陆布局

    render() {
       return (
           <div>
              {/* other things*/}
              <Route path={this.props.match.path} exact="true" component={Home} />
              <Route path={`${this.props.match.path}/login`} component={Login} />
              <Route path={`${this.props.match.path}/signup`} component={Signup} />
           </div>
       )
    }
    

    应用布局

    render() {
       return (
           <div>
              {/* other things*/}
              <Route path={`${this.props.match.path}/dashboard`} exact="true" component={DashboardPage} />
              <Route path={`${this.props.match.path}/users`} component={Login} />
              <Route path={`${this.props.match.path}/u/:username`} component={AccountPage} />
           </div>
       )
    }
    

    【讨论】:

    • 请注意 this.props.match.url 推荐用于 Links。有时人们将this.props.match.path 用于Route 和Link
    • 感谢您提供替代方案,但这不是合适的解决方案。我不能使用像“/landing/home”或“/landing/login”这样的路线。
    • 路由必须是这样的; “/” “/login” “/signup” “/dashboard” “/users” 等等..
    • 基本上我想使用 2 个主布局(视图)。一个用于登录页面,另一个用于应用程序。我怎样才能做到这一点。
    猜你喜欢
    • 2019-02-18
    • 2018-02-16
    • 2016-08-18
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多