0

角版本 9.1.12。

我在加载路由配置(RouteConfigLoadEnd事件)结束时更改了路由配置,代码是这样的:

      if (routerEvent instanceof RouteConfigLoadEnd) {
        let route = (this.router.config[0] as any)._loadedConfig.routes[0];
        if (!route.children) {
          route.children = [];
        }
        if (route.component.name === 'EntranceComponent') {
          if (route.children.length < 1) {
            route.children.push({path: 'ttt', loadChildren: () => import('./entrance2/entrance2.module').then(m => m.Entrance2Module)});
          }
        }
        const cfg = (route.children[0] as any)._loadedConfig;
        if (cfg) {
          route = cfg.routes[0];
          if (!route.children) {
            route.children = [];
          }
          route.children.push({path: 'test1', loadChildren: environment.getModule(1)});
          route.children.push({path: 'test2', loadChildren: environment.getModule(2)});
        }

        // this.router.resetConfig(this.router.config);
      }
是否使用“router.resetConfig”没有区别。

我扩展了 RouteReuseStrategy 并且什么也没做,就像这样:

// custom-route-reuse-strategy.ts

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

export class CustomRouteReuseStrategy extends RouteReuseStrategy {
  retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
    return null;
  }

  shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return false;
  }

  shouldDetach(route: ActivatedRouteSnapshot): boolean {
    return false;
  }

  shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return false;
  }

  store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void {
  }
}

并在 AppModule 中提供,如下所示:

  providers: [
    {
      provide: RouteReuseStrategy,
      useClass: CustomRouteReuseStrategy
    }
  ],

然后,每当我浏览 url 时,都会重新加载 EntranceComponent 和 MenuComponent。组件是这样的:EntranceComponent 直接使用了 MenuComponent: 并且会将 test1 或 test2 组件加载为具有不同路由的子组件。

4

0 回答 0