首页 > 解决方案 > 在以角度 4 重定向到新路线之前获取当前路线的数据

问题描述

实际要求: 在重定向到新路由之前,我想触发当前路由(组件)的功能。此功能实际上是用于保存当前路由中存在的表单的值。(在重定向到新路由(组件)之前自动保存当前组件的数据)。

我的解决方案: 在重定向到新路由之前,我将检查当前路由的数据,该数据将包含当前路由名称和要触发的函数名称。

挑战: 如何在重定向到新路由之前获取当前路由的数据?

下面是示例代码:

路由.ts

export const AppRoutes: Routes = [
          { path: 'overviewHistory', component: OverviewHistoryComponent,data: { state: 'overviewHistory',function:'abc' }},
          { path: 'leadDetails', component: LeadInformationComponent, data: { state: 'leadDetails',function:'abc1' } },
          { path: 'checeklist', component: CheckListComponent,data: { state: 'checeklist',function:'abc2' }},
          { path: 'teamManagement', component: TeamManagementProfileComponent,data: { state: 'teamManagement',function:'abc3' }},
          { path: 'directorDetails', component: DirectorOtherpartyDetailComponent,data: { state: 'directorDetails',function:'abc4' }},
          { path: 'addressDetails', component: AddressDetailsComponent,data: { state: 'addressDetails',function:'abc5' }},
];

HTML:

<div id="site-content" class="content full-width" [@routerTransition]="getState(o)" >
        <router-outlet #o="outlet"  (activate)="optionChange(o)"></router-outlet>

</div>

TS:

  optionChange(outlet){
    // Some logic....
  }

请帮助实现这一目标。还建议是否有任何新方法可以实现实际要求。

标签: angularroutingroutesangular4-router

解决方案


推荐阅读