mat-expansion-panel懒惰渲染是否破碎?

问题描述 投票:1回答:3

this Stackblitz

角度材料mat-expansion-panel允许lazy rendering

<mat-expansion-panel>
  <mat-expansion-panel-header>
    This is the expansion title
  </mat-expansion-panel-header>

  <ng-template matExpansionPanelContent>
    Some deferred content
  </ng-template>
</mat-expansion-panel>

当在隐藏的组件内部渲染时,例如,关闭的mat-sidenavmat-expansion-panel渲染不正确(Stackblitz example):

enter image description here

这在很多方面都是一团糟,尤其是因为我将mat-expansion-panel定义为崩溃(expanded="false"):

        <mat-expansion-panel expanded="false">

            <mat-expansion-panel-header>
                <mat-panel-title>
                    <span>First</span>
                </mat-panel-title>
            </mat-expansion-panel-header>

            <ng-template matExpansionPanelContent>
                <!-- Deferred initialization until the panel is open. -->
                <span>You shouldn't see me yet!</span></ng-template>

            <mat-action-row>
                <button mat-button>Click me</button>
            </mat-action-row>

        </mat-expansion-panel>

单击扩展面板的标题会展开面板(应该如此)并正确呈现内容:

enter image description here

有趣的是,折叠面板显示了面板最初应该如何渲染:

enter image description here

我的Stackblitz示例和此问题中的屏幕截图都显示了mat-accordion中的两个扩展面板:

<mat-tab-group>
    <mat-tab label="Empty">
        <p>This is an empty tab</p>
    </mat-tab>
    <mat-tab label="Accordion">
        <div class="mat-typography">
            <mat-accordion>
                <mat-expansion-panel expanded="true">
                    <mat-expansion-panel-header>
                        <mat-panel-title>
                            <span>First</span>
                        </mat-panel-title>
                    </mat-expansion-panel-header>
                    <ng-template matExpansionPanelContent>
                        <!-- Deferred initialization until the panel is open. -->
                        <span>You shouldn't see me yet!</span></ng-template>
                    <mat-action-row>
                        <button mat-button>Click me</button>
                    </mat-action-row>
                </mat-expansion-panel>
                <mat-expansion-panel expanded="false">
                    <mat-expansion-panel-header>
                        <mat-panel-title>
                            <span>Second</span>
                        </mat-panel-title>
                    </mat-expansion-panel-header>
                    <ng-template matExpansionPanelContent>
                        <!-- Deferred initialization until the panel is open. -->
                        <span>Shouldn't see me either!</span></ng-template>
                    <mat-action-row>
                        <button mat-button>Click me</button>
                    </mat-action-row>
                </mat-expansion-panel>
            </mat-accordion>
        </div>
    </mat-tab>
</mat-tab-group>

CSS只是为调试添加了一些颜色:

mat-expansion-panel {
  background-color: grey;
}

mat-expansion-panel:first-child mat-expansion-panel-header {
  background-color:red;
}
mat-expansion-panel:last-child  mat-expansion-panel-header {
  background-color:blue;
}

mat-expansion-panel button {
  background-color: yellow;
}

mat-sidenav就像它可能得到的那样简单:

<mat-sidenav-container>
    <mat-sidenav #snav class="mat-elevation-z8">
        <app-side-menu></app-side-menu>
    </mat-sidenav>
    <mat-sidenav-content>
        <div>
            <button mat-icon-button (click)="snav.toggle()">
        <mat-icon>menu</mat-icon>
      </button>
        </div>
    </mat-sidenav-content>
</mat-sidenav-container>

更新

关于这方面的冗长讨论发生在GitHub,issues/5269。虽然这个问题仍未解决,但being reported认为问题是在angular/material v7修复的。

javascript angular angular-material
3个回答
1
投票

问题是关于* ngIf。第一次它无法使它成为现实。这就是我使用setTimeout()将其设置为true的原因。

如果您还有问题请告诉我。我会尽力帮忙。

工作环节

https://stackblitz.com/edit/deferred-expansion-panel-broken-b2vurz?file=app%2Fside-menu%2Fside-menu.component.ts


0
投票

这绝对是5.x版本的问题,这应该是开箱即用的。

如果我在html中设置扩展面板[expanded]属性或在构造函数或组件生命周期事件期间初始化,则没有任何效果。

即使是stackblitz的工作示例也不能在动态组件中本地工作! https://stackblitz.com/angular/bbjxooxpqmy?file=app%2Fexpansion-overview-example.html

但是,如果结构指令与异步数据/ Observable一起使用,那么在组件初始化和渲染之后发出的异步数据/ Observable将会起作用......

使用间隔或超时工作,但它不是一个好的解决方案,因为加载和渲染时间在桌面和移动设备等不同设备上差别很大!


0
投票

只是注意到这一点,可能是因为你需要将expanded属性包装在方括号中,否则你传入一串"false",它将评估为true。

它应该是:

<mat-expansion-panel [expanded]="false">
© www.soinside.com 2019 - 2024. All rights reserved.