简体   繁体   English

Angular 6:在子路由中跳过父路由解析器?

[英]Angular 6: Skip parent route resolver in child route?

I'm trying to add routes to my app, and I have a parent route with parentResolver and a child route with Child Resolver.我正在尝试向我的应用程序添加路由,并且我有一个带有 parentResolver 的父路由和一个带有 Child Resolver 的子路由。 When I access the '/parent', the parent resolver kicks in perfectly.当我访问“/parent”时,父解析器完美启动。

But the problem is, when I access '/parent/child', the parent resolver kicks in again before the child resolver can kick in. I don't want to run the parent resolver when I'm navigating to a child page.但问题是,当我访问“/parent/child”时,父解析器会在子解析器启动之前再次启动。我不想在导航到子页面时运行父解析器。

So is there a way to skip the parent resolver when child route is called.那么有没有办法在调用子路由时跳过父解析器。 Here's my route config这是我的路线配置

{
    path: 'parent',
    component: ParentComponent,
    resolve: {parentData: ParentResolver},
    runGuardsAndResolvers: 'paramsOrQueryParamsChange',
    children: [
      {
        path: 'child',
        component: ChildComponent,
        resolve: {childData: ChildResolver},
        runGuardsAndResolvers: 'paramsOrQueryParamsChange'
      }
    ]
  }

Try the following:请尝试以下操作:

{
    path: 'parent',
    children: [
               {  path : '',
                  pathMatch : 'full',
                  component: ParentComponent,
                  resolve: {parentData: ParentResolver},
                  runGuardsAndResolvers: 'paramsOrQueryParamsChange',
               },
               {  path: 'child',
                  component: ChildComponent,
                  resolve: {childData: ChildResolver},
                  runGuardsAndResolvers: 'paramsOrQueryParamsChange'
               }
             ]
}
  1. Check in parent resolver what is target path.检查父解析器什么是目标路径。 (or if ActivatedRouteSnapshot has children) (或者如果 ActivatedRouteSnapshot 有孩子)

     resolve(route: ActivatedRouteSnapshot) { if (route.children.length) { // target is some child } else { // target is parent }

    } }

  2. Create standalone path: 'parent/child'创建独立路径:“父/子”

You can create what I call a ResolverGuard, which is a Guard that acts as a Resolver.您可以创建我所说的 ResolverGuard,它是充当解析器的 Guard。

{
path: 'parent',
component: ParentComponent,
resolve: [ParentResolver], // simply return an Observable<boolean>
runGuardsAndResolvers: 'paramsOrQueryParamsChange',
children: [
  {
    path: 'child',
    component: ChildComponent,
    canActivate: [ChildResolverGuard],
    runGuardsAndResolvers: 'paramsOrQueryParamsChange'
  }
]

} }

However, this will simply run the child guard before the parent Resolver.然而,这只会在父解析器之前运行子保护。 The resolver will run regardless, but you can add logic in the ParentResolver to skip it if the child Guard ran.无论如何,解析器都会运行,但是您可以在 ParentResolver 中添加逻辑以在子 Guard 运行时跳过它。

Checkout https://github.com/angular/angular/issues/20805#issuecomment-350106463结帐https://github.com/angular/angular/issues/20805#issuecomment-350106463

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

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