【问题标题】:mat-accordion is lagging when calling angular component in it recursively递归调用角组件时,mat-accordion 滞后
【发布时间】:2020-05-07 08:37:40
【问题描述】:

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

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

它就像一棵 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 之前的代码所示),但效果不佳。

打开手风琴有没有办法加载组件?

谢谢!

【问题讨论】:

    标签: angular recursion tree components angular8


    【解决方案1】:

    回答你的问题:

    打开手风琴有没有办法加载组件?

    是的,有,但我不确定它是否能解决滞后问题。无论如何,尝试将扩展面板的内容包装在包含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>
    

    【讨论】:

    • 谢谢!!....它工作了!...这也解决了大容量数据的滞后问题,因为 ng-template 限制手风琴加载数据,除非它被扩展。我很感激你。
    猜你喜欢
    • 2018-10-02
    • 2023-03-09
    • 2015-09-08
    • 2021-12-09
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2021-10-04
    • 2011-07-06
    相关资源
    最近更新 更多