加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2 – ngOnDestroy()未在类似路线上调用

发布时间:2020-12-17 17:39:57 所属栏目:安全 来源:网络整理
导读:我有一个Angular2应用程序,其路径如下: { path: '',component: ContentComponent,children: [ { path: 'folder/:folderId',resolve: { currentFolder: CurrentFolderResolver,},children: [ { path: '',resolve: { folderStructure: FolderStructureResolve
我有一个Angular2应用程序,其路径如下:

{
  path: '',component: ContentComponent,children: [
    {
      path: 'folder/:folderId',resolve: {              
        currentFolder: CurrentFolderResolver,},children: [
        {
          path: '',resolve: {
            folderStructure: FolderStructureResolve,component: FolderOverviewComponent,{
          path: 'users',component: UsersComponent,}
      ]
    }
  ]
}

从/ folder / 123之类的路径导航到/ folder / 456时,Angular不会触发FolderOverviewComponent中的ngOnDestroy().导航到/ folder / 456 / users会这样做.

换句话说,如果路径没有改变,似乎Angular不会破坏组件(忽略:folderId的动态部分).这似乎是合理的,但我需要在ngOnDestroy()中清理一些东西.

每次导航到新路线(即使用不同的参数)时,是否可以将路由配置为调用destroy?

解决方法

这是设计的.如果只有路径参数发生变化而导致使用相同的路径,则不会销毁和重新创建组件,但会重复使用.

您可以订阅params更改,以便在路由更改时能够执行代码:

constructor(router: ActivatedRoute) {
  router.params.subscribe(param => routeChanged(param['folderId']));
}

有计划在未来提供更多的灵活性,但目前这是可行的方法.

另见https://angular.io/guide/router#activated-route-in-action

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读