简体   繁体   English

导航到第一个孩子时触发父路由 OnInit

[英]Parent route OnInit triggers when navigating to first child

I have an issue that when I change route to a child route, the parent route's OnInit triggers.我有一个问题,当我将路由更改为子路由时,父路由的OnInit会触发。 If I have 3 children, the parent will init maximum 3 times (each for children, when I navigate to the children on the first time).如果我有 3 个孩子,父母将最多初始化 3 次(每次针对孩子,当我第一次导航到孩子时)。

I know it happens due to my CustomReuseStrategy but I dont know exactly why and how to fix it (I copied the CustomReuseStrategy from somewhere else)我知道这是由于我的CustomReuseStrategy而发生的,但我不知道确切的原因以及如何解决它(我从其他地方复制了 CustomReuseStrategy)

A sandbox of the above problem上述问题的一个沙箱

I was able to solve it using a different custom reuse strategy我能够使用不同的自定义重用策略来解决它

import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';


export class CustomReuseStrategy implements RouteReuseStrategy {
  public static handlers: { [key: string]: DetachedRouteHandle } = {};

  public shouldDetach(route: ActivatedRouteSnapshot): boolean {
    return true;
  }

  public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
    CustomReuseStrategy.handlers[this.getRouteUrl(route)] = handle;
  }

  public shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return !!CustomReuseStrategy.handlers[this.getRouteUrl(route)];
  }

  public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
    if (!route.routeConfig) {
      return null;
    }

    return CustomReuseStrategy.handlers[this.getRouteUrl(route)];
  }

  public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return future.routeConfig === curr.routeConfig && JSON.stringify(future.params) === JSON.stringify(curr.params);
  }

  private getRouteUrl(route: ActivatedRouteSnapshot) {
    return route['_routerState'].url.replace(/\//g, '_');
  }
}

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

相关问题 Angular 子组件在父 oninit 期间父函数未初始化 - Angular child component not initialised when parent is function during parent oninit 当有Parent Component和Child Component时,为什么在Parent OnInit之前调用Child Constructor? - When there is a Parent Component and a Child Component, why is the Child Constructor called before the Parent OnInit? Angular 4从子OnInit检测路线? - Angular 4 Detecting route from child OnInit? 导航到子模块延迟加载的路由时,父模块组件错误 - Parent Module Components Error When Navigating To SubModule Lazy Loaded Route Angular 子路由在直接加载时重定向,在那里导航工作正常 - Angular child route redirects when loaded directly, navigating there works fine 角度路由未导航到子路由 - Angular Routing is not navigating to child route 角卫士导航到父路线 - Angular guards for navigating to a parent route 在 Angular 8 路由父路由与其他父路由导航时保持不变 - In Angular 8 Router parent route remains the same when navigating to the route with other parent 在子路径中导航时为什么激活了父路由器? - Why parent router is activated when navigating through child routes? 导航到子组件时父组件重新初始化 - Parent component getting re-initialised when navigating to child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM