- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要制作一个简单的确认窗口,我看到了很多如何通过额外操作来完成此操作的示例(例如等待表单文件上传不存在)。但我只需要创建一个带有默认文本的默认确认窗口(如下图所示),以便在用户想要离开当前页面时显示它。我无法完全理解在处理 before unload
事件时应该证明什么逻辑。
我最近很抱歉,如果它是某些问题的重复,但是,我没有找到任何解决方案。所以我有:
example.guard.ts
export interface CanComponentDeactivate {
canDeactivate: () => Observable<boolean> | boolean;
}
@Injectable()
export class ExampleGuard implements CanDeactivate<CanComponentDeactivate> {
constructor() { }
canDeactivate(component: CanComponentDeactivate): boolean | Observable<boolean> {
return component.canDeactivate() ?
true :
confirm('message'); // <<< does confirm window should appear from here?
}
}
example.component.ts
export class ExampleComponent implements CanComponentDeactivate {
counstructor() { }
@HostListener('window:beforeunload', ['$event'])
canDeactivate($event: any): Observable<boolean> | boolean {
if (!this.canDeactivate($event)) {
// what should I do here?
}
}
}
如果您提供代码示例,那就太好了,但我很感谢任何形式的帮助。
最佳答案
您应该区分 window
上的 beforeunload
native 事件和 canDeactivate 防护。当您尝试关闭选项卡/窗口时会触发第一个。这样,当它被触发时,您可以confirm(...)
用户并对其执行event.preventDefault()
以取消关闭选项卡/窗口。
谈论 CanDeactivate
守卫,它应该返回一个 boolean 的 observable/promise/plain-value ,它会告诉你是否可以停用当前路由。
因此,最好将两种方法分开(一种用于 beforeunload
,另一种用于保护)。因为如果您希望将行为更改为不仅使用 native 确认,还使用自定义模式窗口,则 beforeunload
的默认事件处理程序将无法工作,因为它处理同步代码。因此,对于beforeunload
,您只能使用confirm
来要求用户不要离开页面。
loading = true;
@HostListener('window:beforeunload', ['$event'])
canLeavePage($event: any): Observable<void> {
if(this.loading && confirm('You data is loading. Are you sure you want to leave?')) {
$event.preventDefault();
}
}
另一方面,Guard 希望返回 bool 值(或 Promise, 或可观察)。所以在这里你可以只返回你的条件的结果:
canDeactivate(): boolean {
return this.loading && confirm('You data is loading. Are you sure you want to leave?');
}
因此,在您的 CanDeactivate
防护中,它将像 return component.canDeactivate()
关于 Angular 6 : simple confirmation on page leaving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54199903/
我想创建一个对象,比方说一个饼图。 class Pie def initialize(name, flavor) @name = name @flavor = flavor e
我正在寻找类似于 Log property in Linq DataContext 的功能. 最佳答案 所有执行的 SQL 都写入 Trace,因此您可以使用 TraceListener 访问。 关于
使用 django-simple-history ,如何从我的模型中获取最后更改的对象? 我尝试了 MyModel.history.most_recent(),它需要一个模型实例,因此可能会返回所选实
我定期使用 Eclipse 调试器,这一直困扰着我关于 IDE 的问题。步进过滤是一个非常宝贵的工具,这样我就不会进入没有源代码或者我根本不感兴趣的类。但是,Eclipse 在大多数情况下都没有正确处
我基于有向图构建一棵树。源数据是 SQL 表中的一系列父子关系。它肯定是一棵树(无论如何我都会验证)。我想要一组从根到每片叶子的简单路径。数据是会计“科目表”中的标题,路径类似于“根 -> Asset
我对如何在torii中使用ember-simple-auth有点困惑 我正在使用ember-cli-simple-auth和ember-cli-simple-auth-torii 我得到下面提到的错误
考虑以下用 simple-xml 注释注释的枚举: @Root(name="days") public enum DaysOfWeek { SUNDAY("blue", 30), MO
我有一个 json 文件,我正在 excel 中创建一些 vba 代码。我想用java读取这个json文件。为此,我编写了以下代码 try { Class.forName("org.j
我在尝试使用 google 的 simple-json 解析一个简单的 json 时遇到了一个奇怪的问题。 这是我的代码,它不起作用: String s = args[0].toString(); J
我正在尝试解析下面的 json 文件: {"units":[{"id":42, "title":"Hello World", "positi
我正在使用 simple-import-sort eslint 插件进行 react 。我想我的 .eslintrc.js是对的,但我无法使这个特定的插件工作。我在文件的第一行收到以下错误: 未找到规
我正在尝试解析子文件,但我不知道自己做错了什么(当然,我也不知道自己做对了什么)。 文件.json [{ "arrOne":{ "one":"a", "two":"b",
在我的Angel 16(独立的)应用程序中,我有一个简单的服务来测试LangChain和OpenAI。同样的代码在NodeJS应用程序中运行得非常好,然而,当从角度运行时,OpenAI返回的结果总是空
我正在尝试测试连接到数据库的 Web 应用程序 - 我的 Junit 代码不执行连接。 设置按照 https://github.com/h-thurow/Simple-JNDI InitialCont
我正在尝试制作一个小型应用程序,它可以通过 ISBN 在亚马逊上搜索一本书。我是 Amazon Web Service 的新手。 我正在关注以下链接: http://flyingpies.wordpr
我正在使用简单 XML 序列化 (simple-xml-2.6.6.jar) here将我的 XML 响应从 webservice 转换为 POJO 类。 XML 是:
Simple UI混合开发的必经之路~ Simple UI快速上手 在混合开发的模式下,如果想使用django admin,又嫌弃后台不符合你的审美?Simple UI给你想要的答案,我不是打广告的~
Simple JSON是Google开发的Java JSON解析框架,基于Apache协议。 下载的文件是:json_simple.jar 例子1:很方便的方式,使用JSONValue
我有一个 simple_form 表单设置,它会很好地显示内联错误。我遇到过一些用户看不到这些错误的问题,并要求在非常长的表格顶部进行清晰的枚举。我使用了 Rails 教程中的代码设置:
我正在使用简单的 XML 框架,只是重命名了一些 XML 布局,这些布局现在似乎不再起作用了。 这是我的 XML: 2 0 1
我是一名优秀的程序员,十分优秀!