Angualr中beforeunload警告框的自定义文本
创始人
2024-10-14 10:01:17
0
  1. 创建一个服务来自定义警告框文本,例如:

import { Injectable } from '@angular/core';

@Injectable() export class AlertService { message: string;

constructor() {}

setMessage(msg: string) { this.message = msg; }

getMessage() { return this.message; } }

  1. 在组件中,注入该服务并在load或unload事件上注册窗口beforeunload事件,例如:

import { Component, OnInit, HostListener } from '@angular/core'; import { AlertService } from './alert.service';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'custom-beforeunload-alert'; message: string;

constructor(private alertService: AlertService) {}

ngOnInit() { this.alertService.setMessage('确认离开吗?'); }

@HostListener('window:beforeunload', ['$event']) unloadNotification($event: any) { const message = this.alertService.getMessage(); if (message) { $event.returnValue = message; } } }

  1. 在模板中显示警告框文本,例如:

{{alertService.getMessage()}}

相关内容

热门资讯

分享开挂内幕!hhpoker可... 1、分享开挂内幕!hhpoker可以开挂吗,werplan怎么作弊,详细教程(有挂软件);详细教程。...
2024教程!德普之星辅助器怎... 《2024教程!德普之星辅助器怎么用,约局吧app有挂吗,透牌教程(有挂透视)》 德普之星辅助器怎么...
记者揭秘!pokermaste... 记者揭秘!pokermaster脚本,大菠萝免费辅助器,2025新版总结(有挂方法)1、点击下载安装...
今日头条!hhpoker免费辅... 今日头条!hhpoker免费辅助器,pokemmo手机版透视脚本,wepoke教程(有挂方法);致您...
分享个大家!哈糖大菠萝破解器,... 分享个大家!哈糖大菠萝破解器,we poker游戏下,新版2025教程(有挂技巧);亲真的是有正版授...
一分钟揭秘!pokemmo内置... 一分钟揭秘!pokemmo内置修改器,xpoker辅助器,可靠教程(有挂方法)关于pokemmo内置...
揭秘关于!德州hhpoker脚... 揭秘关于!德州hhpoker脚本,wepoker插件下载,解密教程(有挂软件);玩家必备必赢加哟《1...
每日必看推荐!拱趴大菠萝开挂方... 每日必看推荐!拱趴大菠萝开挂方法,we poker免费辅助器,详细教程(有挂神器)是一款可以让一直输...
新手必备!wepoker买钻石... 新手必备!wepoker买钻石有用吗,wepoker透视苹果系统,介绍教程(有挂辅助)1、点击下载安...
分享认知!hhpoker万能辅... 分享认知!hhpoker万能辅助器,wejoker黑侠辅助器,可靠教程(有挂神器)准备好在hhpok...