在页面刷新时保持mat-expansion面板的"expanded"状态,可以通过以下步骤实现:
mat-accordion
组件包裹你的mat-expansion-panel
,创建一个可折叠面板的容器。<mat-accordion>
<mat-expansion-panel [expanded]="isExpanded">
<!-- 面板的内容 -->
</mat-expansion-panel>
</mat-accordion>
import { Component, OnInit } from '@angular/core';
import { MatExpansionPanel } from '@angular/material/expansion';
import { StorageService } from 'your-storage-service-path'; // 导入本地存储服务
@Component({
selector: 'your-component',
templateUrl: './your-component.html',
styleUrls: ['./your-component.css']
})
export class YourComponent implements OnInit {
isExpanded: boolean;
constructor(private storageService: StorageService) {}
ngOnInit(): void {
this.isExpanded = this.storageService.getItem('panelState') === 'expanded';
}
togglePanelState(panel: MatExpansionPanel): void {
this.isExpanded = panel.expanded;
// 在状态改变时,将面板展开状态保存到本地存储
this.storageService.setItem('panelState', this.isExpanded ? 'expanded' : 'collapsed');
}
}
togglePanelState
方法。<mat-accordion>
<mat-expansion-panel [expanded]="isExpanded" (opened)="togglePanelState($event.panel)" (closed)="togglePanelState($event.panel)">
<!-- 面板的内容 -->
</mat-expansion-panel>
</mat-accordion>
通过以上步骤,你可以实现在页面刷新时保持mat-expansion
面板的"expanded"状态。在每次展开或折叠面板时,会将面板的展开状态保存到本地存储,并在页面刷新时重新加载状态。这样,用户在页面刷新后仍然可以看到之前展开的面板。
领取专属 10元无门槛券
手把手带您无忧上云