扩展路线时更改路线保护

Sterling Store Engagement 使用 Angular 路由保护器来控制基于条件的某些路由的导航。 因此,当您扩展路由时,会对路由保护器进行某些更改。

例如,ValidatePackOrderGuard 文件中的 store-single-spa/packages/features/pack-order/src/app/features/pack-order/pack-order-page/validate-pack-order.guard.ts angular 路由保护用于在您转到 Pack 产品页面之前验证货物是否处于有效状态。 如果货件未处于有效状态,则不会显示打包产品页面。

在扩展任何受路由保护的路由时,都需要更改 Angular 路由保护。 例如,如果您扩展了包流程中的 PackOrderPageComponent,请修改 ValidatePackOrderGuard angular 路由保护。

要修改 ValidatePackOrderGuard angular 路由保护,请完成以下步骤:
  1. 在路由使用的数据服务文件中,添加 routingInProgress$ 主题。 在本例中,PackOrderPageDataService 就是数据服务。
    import { Subject } from 'rxjs';
    public routingInProgress$: Subject<boolean> = new Subject<boolean>();
  2. 在路由保护中,添加 routingInProgress 变量,如下所示:
    public routingInProgress = false;
  3. 将代码包含在 if(!this.routingInProgress) 块中的 canActivate 方法中。
  4. routingInProgress) 设置为 true
  5. 在承诺处理程序中,将 routingInProgress 重置为 false,并发出 routingInProgress$.next(false)
  6. 在 else 块中添加 this._location.back(); 语句,如以下代码片段所示。 您可以在 //Extension changes - start ////Extension changes - end // 注释中找到更改。
    /*******************************************************************************
    * IBM Confidential
    * IBM Sterling Order Management (5737-D18)
    * IBM Sterling Order Management Software (5725-D10)
    * (C) Copyright IBM Corp. 2022
    ******************************************************************************/
    
    import { Location } from '@angular/common';
    import { Injectable } from '@angular/core';
    import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, UrlTree } from '@angular/router';
    import { routePath, UIUtil } from '@store/core';
    import { PackOrderPageDataService } from './pack-order-page-data.service';
    
    @Injectable()
    export class ValidatePackOrderGuard implements CanActivate {
    
      private shipmentId = '';
      // Extension changes - start //
      public routingInProgress = false;
       // Extension changes - end //
      constructor(
        private packOrderDataService: PackOrderPageDataService,
        private router: Router,
        private _location: Location
      ) { }
    
      canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): Promise<boolean | UrlTree> | boolean | UrlTree {
    
        this.shipmentId = next.params['ShipmentNo'];
        //Extension changes - start //
        if(!this.routingInProgress){
        this.routingInProgress = true; 
        this.packOrderDataService.routingInProgress$.next(true);
        //Extension changes - end //
        return this.packOrderDataService.validatePackReqBeforePack(this.shipmentId)
          .then(
            validationStatus => {
             let validationState = validationStatus['Shipment'] && !validationStatus['Shipment'].Error ? 'continueToPack' : validationStatus;
             //Extension changes - start //
              this.routingInProgress = false;
              this.packOrderDataService.routingInProgress$.next(false);
             //Extension changes - end //
              if (validationState === 'continueToPack') {
                return true;
              } else if (validationStatus['Shipment'] && validationStatus['Shipment'].Error && validationStatus['Shipment'].Error.ErrorDescription) {
                const navigationURL = routePath(`shipment/summary/${this.shipmentId}`, 'shell');
                const queryParams = { errorMsg: validationStatus['Shipment']?.Error?.ErrorDescription };
                if (this.router.url === '/') {
                  history.replaceState('', '', '/' + navigationURL + UIUtil.jsonToQueryString(queryParams));
                }
                return this.router.createUrlTree([navigationURL], { queryParams: queryParams });
              } else {
                if (this.router.url === '/') {
                  this._location.back();
                  return true;
                } else {
                  //Extension changes - start //
                  this._location.back();
                  //Extension changes - end //
                  return false;
                }
              }
            }
          );
        }
      }
    }
  7. 在路由组件 ngOnInit 方法中订阅 routingInProgress$ 。 例如,在 PackOrderPageComponent
    this.dataService.routingInProgress$.subscribe(routingInProgress => {
     if(!routingInProgress){
      this.packAllPermission = ResourcePermissionUtil.hasPermission(this.PACKALL_RESOURCE_PERMISSION);
      this.showItemImage = RulesUtil.isRuleEnabled('WSC_SHOW_ITEM_IMAGES');
      this.shipmentKey = this.activeRoute.snapshot.params['ShipmentNo'];
      this.getInitializationDataForShipment(true);
     }
     });