mat-accordion 在递归调用其中的 angular 组件时滞后

mat-accordion is lagging when calling angular component in it recursively

我在手风琴中递归调用组件以显示树状结构。

代码适用于短输入,但在嵌套数据的递归调用上滞后

它就像一棵 json 树。

//sample.component.html
<app-tree [input]="data"></app-tree>


//tree.component.html

    <mat-accordion>
        <mat-expansion-panel hideToggle="true" (click)="toggleTree($event, input)">
          <mat-expansion-panel-header>
            <mat-panel-title>
                <mat-icon>{{input.buttonName}}</mat-icon>
                {{input.key}}
            </mat-panel-title>
          </mat-expansion-panel-header>
          <ul class="main-obj">   // tried *ngIf here
             <div class="obj-list" *ngFor="let item of input.value; trackBy: trackChanges">
                <li>
                  {{item.key}}: {{item.value}}
                </li>
                <li style="list-style: none; margin-left: -40px;">
                    <app-tree [input]="item"></app-tree>
                </li>
            </div>
         </ul>
        </mat-expansion-panel>
    </mat-accordion>

//tree.component.ts

    import { Component, OnInit, Input, Output } from '@angular/core';
    import { EventEmitter } from 'events';

    @Component({
      selector: 'app-tree',
      templateUrl: './tree.component.html',
      styleUrls: ['./tree.component.css']
    })

    export class TreeComponent implements OnInit {
        @Input() input: any;

        @Output() click = new EventEmitter();

        constructor(){}

        ngOnInit(){
        }

        toggleTree(event, toggleButton) {
            event.stopPropagation();
            if(toggleButton.buttonName == 'add')  
                toggleButton.buttonName = 'remove';
            else
                toggleButton.buttonName = 'add';
        };

        trackChanges = (index) => {
            return index;
        }
    }

我试过放置 ngIf(如 ngFor 之前的代码所示)但效果不佳。

打开手风琴时有没有加载组件的方法?

谢谢!

回答您的问题:

Is there any way to load component when opening accordion?

是的,有,但我不确定它是否会解决滞后问题。无论如何,尝试将扩展面板的内容包装在包含 matExpansionPanelContent 属性的 ng-template 中。它会延迟加载其内容(参见 the docs)。

<mat-accordion>
  <mat-expansion-panel hideToggle="true" (click)="toggleTree($event, input)">
    <mat-expansion-panel-header>
      <mat-panel-title>
        <mat-icon>{{input.buttonName}}</mat-icon>
        {{input.key}}
      </mat-panel-title>
    </mat-expansion-panel-header>

    <ng-template matExpansionPanelContent>
      <ul class="main-obj">   // tried *ngIf here
        <div class="obj-list" *ngFor="let item of input.value; trackBy: trackChanges">
          <li>{{item.key}}: {{item.value}}</li>
          <li style="list-style: none; margin-left: -40px;">
            <app-tree [input]="item"></app-tree>
          </li>
        </div>
      </ul>
    </ng-template>
  </mat-expansion-panel>
</mat-accordion>