【发布时间】:2021-08-19 22:10:45
【问题描述】:
我正在开发一个 Angular 单页应用程序,当用户单击按钮时,页面会刷新并显示一条消息。
(打字稿)
import { Component, OnInit, OnDestroy } from '@angular/core';
export class newComponent implements OnInit, OnDestroy {
showAlertMessage = false;
buttonClicked(dataElement: DataModel): void {
this.restService.postAction(dataElement, 'a new record is being inserted').subscribe();
window.location.reload();
this.showAlertMessage = true;
}
(HTML)
<table mat-table [dataSource]="dataSource" multiTemplateDataRows class="mat-elevation-z8">
<th mat-header-cell *matHeaderCellDef> Header </th>
<td mat-icon *matCellDef="let element">
<button (click)="buttonClicked(element)">No Data To Report</button>
</td>
</table>
<div class="alert alert-primary" *ngIf="showAlertMessage" role="alert">
This is a primary alert—check it out!
</div>
我遇到的问题是当用户单击时,警报消息会短暂显示,然后在刷新页面时消失。如何重新加载页面,然后显示消息?目前在 buttonClicked() 方法中发生 window.location.reload() 后,该消息不显示。
【问题讨论】:
-
重新加载页面后,内存中的所有上下文都丢失了。要解决此问题,您必须将数据存储在本地存储或 cookie 或其他方法中,例如在 URL 中将值作为参数传递。
标签: javascript angular typescript