有没有办法在每个 mat-step 中延迟加载组件?

FZa*_*man 3 angular mat-stepper

我正在尝试实现一个 mat-stepper,它在每个步骤中都有不同的组件。我需要将数据从第一步传递到下一步,所以我基本上使用 @ViewChild 和 @Input 来做到这一点。但是,我希望每个步骤中的组件仅在控件移动到该步骤时才被初始化,而不是在最开始时。我尝试使用其中的组件,就像我们如何延迟加载 mat-tabs 一样,但它似乎不起作用。有解决方法吗?

Aur*_*ien 12

作为参考, Angular Material 文档mat-step中描述了一种内置的延迟渲染方法。

如果您希望将某些内容推迟到特定步骤打开,则可以将其放入具有 matStepContent 属性的 ng-template 中。

这是一个例子:

<mat-vertical-stepper>
    <mat-step>
        <ng-template matStepLabel>Step 1</ng-template>
        <ng-template matStepContent>
            <p>This content was rendered lazily</p>
            <button mat-button matStepperNext>Next</button>
        </ng-template>
    </mat-step>
<mat-vertical-stepper>
Run Code Online (Sandbox Code Playgroud)


小智 5

你可以使用类似的东西

<mat-vertical-stepper #stepper>
   <mat-step #first_step>
      <ng-container *ngIf="stepper.selected == null || stepper.selected == first_step">
      </ng-container>
   </mat-step>
   <mat-step #second_step>
      <ng-container *ngIf="stepper.selected == second_step">
      </ng-container>
   </mat-step>
   <mat-step #third_step>
      <ng-container *ngIf="stepper.selected == third_step">
      </ng-container>
   </mat-step>
</mat-vertical-stepper>
Run Code Online (Sandbox Code Playgroud)

请注意,第一步的条件也需要包括对 null 的检查。否则,您将收到 ExpressionChangedAfterItHasBeenCheckedError。

编辑

你也可以这样

<mat-vertical-stepper #stepper>
       <mat-step>
          <ng-container *ngIf="stepper.selected === 0 ">
          </ng-container>
       </mat-step>
<mat-vertical-stepper/>
Run Code Online (Sandbox Code Playgroud)

当然,“0”与第一步有关。