Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angular – 子模块中的RouteReuseStrategy大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的懒惰加载子模块:

@NgModule({
  imports: [
    CommonModule,RouterModule.forChild(acnpRoutes),....
  ],declarations: [...],providers: [
    {provide: RouteReuseStrategy,useClass: ACNPReuseStrategy}
  ]
})
export class AddCustomerNaturalPersonModule {
}

路线:

const acnpRoutes: Routes = [
  {
    path: '',component: AddCustomerNaturalPersonComponent,children: [
      {
        path: 'stepOne',component: ACNPStepOneComponent
      },{
        path: 'stepTwo',component: ACNPStepTwoComponent
      },]
  }
]

和ACPNReuseStrategy:

export class ACNPReuseStrategy implements RouteReuseStrategy {
  handlers: {[key: String]: DetachedRouteHandlE} = {}

  shouldDetach(route: ActivatedRouteSnapshot): Boolean  {
    console.log(1)
    return true;
  }

  store(route: ActivatedRouteSnapshot,handle: {}): void {
    console.log(2)
  }

  ...

  shouldReuseRoute(future: ActivatedRouteSnapshot,curr: ActivatedRouteSnapshot): Boolean {
    console.log(5)
  }
}

不幸的是,ACNPReuseStrategy方法中的这些console.log都没有被触发.这是为什么?是否可以在延迟加载的模块中重用组件?

解决方法

TL; DR在主模块中提供RouteReuseStrategy而不是子模块(认情况下为app.module.ts).然后,为每个路由分配route.data中的密钥以区分您的路由.

我最近也遇到过这个问题.我的子模块安装在主应用程序路由下,如下所示:

...,{    // in app.route.ts
          path: 'apimarket',canActivate: [DeveloperResolve],loadChildren: './apimarket/apimarket.module#ApiMarketModule'
}

如果我在子模块ApiMarketModule中提供自定义的RouteReuseStrategy,则永远不会构造RouteReuseStrategy.

解决方案是在主模块中提供策略而不是子模块(在我的情况下为app.module.ts).然后你的RouteReuseStrategy将被正确构建.

但是,由于route.routeConfig.path是因为您的子路由而相对路径,因此策略将无法按预期工作.要解决这个问题,我的解决方案是为我的路线分配一个唯一的键,如下所示:

export const apimarketRoutes: Routes = [
    {
        path: '',component: ApiMarketComponent,data: {
            shouldReuse: true,key: 'apimarketroot'
        }
    },{
        path: ':id',component: ContentPageComponent,}
];

这是我的RouteReuseStrategy实现FYR

export class MyRouteReuseStrategy implements RouteReuseStrategy {
  handlers: {[key: String]: DetachedRouteHandlE} = {};

  constructor() {
    console.log('constructed');
  }

  retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
    if (!route.data['shouldReuse']) {
      return null;
    }
    console.log('Attach cached page for: ',route.data['key']);
    return this.handlers[route.data['key']];
  }

  store(route: ActivatedRouteSnapshot,handle: DetachedRouteHandlE): void {
    if (route.data['shouldReuse']) {
      this.handlers[route.data['key']] = handle;
    }
  }

  shouldDetach(route: ActivatedRouteSnapshot): Boolean {
    return !!route.data['shouldReuse'];
  }

  shouldAttach(route: ActivatedRouteSnapshot): Boolean {
    return !!route.data['shouldReuse'] && !!this.handlers[route.data['key']];
  }

  shouldReuseRoute(future: ActivatedRouteSnapshot,current: ActivatedRouteSnapshot): Boolean {
    return !!future.data['shouldReuse'];
  }
}

(RouteReuseStrategy没有详细记录,由于策略在根级别工作,我的解决方案可能存在潜在的性能问题.欢迎讨论:))

大佬总结

以上是大佬教程为你收集整理的angular – 子模块中的RouteReuseStrategy全部内容,希望文章能够帮你解决angular – 子模块中的RouteReuseStrategy所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。