简体   繁体   English

用普通路由反应多个布局(路由根)

[英]react multiple layouts (route roots) with plainroutes

I'm trying to use plainroutes in order to create a react-router profile. 我正在尝试使用plainroutes来创建一个react-router配置文件。 I'm not convinced plainroutes is the most readable way to write code, but I'm trying to give it a fair shot since everyone seems excited about it. 我不认为plainroutes是编写代码的最易读的方法,但是由于每个人都对此感到兴奋,因此我想尝试一下。 I'm having an extremely frustrating time trying to define multiple layouts for my components. 我在尝试为组件定义多个布局时感到非常沮丧。 Take the following working code: 采取以下工作代码:

Working plainroutes example 工作普通路线示例

export const createRoutes = (store) => ({
  path        : '/',
  component   : CoreLayout,
  childRoutes : [
    LoginView(store),
    SignupView(store),
    Activate(store),
    ForgotPw(store),
    ConfirmReset(store)
  ]
}
)

Nothing unexpected happening here. 这里没有任何意外发生。 The actual routes are built into the views using the following structure (LoginView for example): 使用以下结构(例如,LoginView)将实际路由内置到视图中:

Directory structure of childRoute objects childRoute对象的目录结构

-/Login
--index.js
--/components
--Loginview.jsx
--Loginview.scss

The index.js files contain little route blocks looking like this: index.js文件包含一些路由块,如下所示:

Example childroute 子路线示例

export default (store) => ({
  path : 'activate',
  component: ActivateView
})

I'll also include the source of the Login component below, as referred to above. 如上所述,我还将在下面包括“ Login组件的来源。 Please note I did try adding path: 'login' to this component but it made no difference. 请注意,我确实尝试向该组件添加path: 'login' ,但没有任何区别。

Login import 登录导入

export default {
  component: LoginView
}

When a user visits /login they see the login component. 用户访问/login他们会看到登录组件。 Easy right? 容易吧? Yep. 是的 Now you might notice all those routes look like a group of authentication-related views. 现在您可能会注意到所有这些路由看起来像一组与身份验证相关的视图。 I want those views to share a layout. 我希望这些视图共享布局。 In this case, CoreLayout . 在这种情况下,为CoreLayout That's also working with the above code. 上面的代码也可以使用。

Next step is that I want to add a user dashboard for after users login. 下一步是我要在用户登录后添加用户仪表板。 This dashboard needs to use a different layout which I'm calling LoggedIn . 该仪表板需要使用我称为LoggedIn的不同布局。 Naturally, I expected that adding another json object with a similar pattern could accomplish this, so I produced the below code: 自然,我希望添加另一个具有类似模式的json对象可以完成此操作,因此我生成了以下代码:

Broken multiple layout attempt 多次布局尝试失败

export const createRoutes = (store) => ({
  path        : '/login',
  component   : CoreLayout,
  indexRoute  : Login,
  childRoutes : [
    SignupView(store),
    Activate(store),
    ForgotPw(store),
    ConfirmReset(store)
  ]
},
  {
    path        : '/',
    component   : LoggedIn,
    indexRoute  : Home,
    childRoutes : [
    ]
  }
)

The above code does not work as intended. 上面的代码无法正常工作。 The only path that works are that paths in the second element of the set (the one with the / route). 唯一有效的路径是集合的第二个元素(带有/路由的元素)中的路径。 I tried moving some routes from the first element down and the do work when put in the second element... but this obviously doesn't solve my problem. 我尝试将一些路线从第一个元素下移,然后在放入第二个元素时进行工作...但这显然不能解决我的问题。

The most frustrating thing to me is that it seems to me as if I am following the SO posts which deal with this, though its a little difficult to tell for sure because they don't use plainroutes. 对我来说,最令人沮丧的是,我似乎正在关注处理该问题的SO帖子,尽管由于它们不使用普通路由,所以很难确定这一点。 I'm linking one here for reference. 我在这里链接一个以供参考。

Typing this out actually helped me work through the problem. 键入实际上可以帮助我解决问题。 Nothing like a good rubber duck. 没有什么比一个好的橡皮鸭更合适了。 It looks like I misunderstood the nature of the router object. 看来我误解了路由器对象的性质。 Apparently it needs to be a legitimate object, where I was under the impression it was accepting a collection. 显然,它必须是一个合法的对象,在我印象中它正在接受一个集合。 Thus, you need to define everything within the scope of a single parent. 因此,您需要在单亲范围内定义所有内容。 See below for how I solved it for my particular example: 请参见下文,了解如何针对特定示例解决此问题:

export const createRoutes = (store) => (
  {
    path        : '/',
    component   : CoreLayout,
    indexRoute  : Home,
    childRoutes : [
      {
        path        : '/',
        component   : AuthLayout,
        childRoutes : [
          LoginView(store),
          SignupView(store),
          Activate(store),
          ForgotPw(store),
          ConfirmReset(store)
        ]
      },
      {
        path        : '/admin',
        component   : LoggedIn,
        indexRoute  : Home,
        childRoutes : [
        ]
      }
    ]
  }

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM