【问题标题】:CanDeactivate confirm messageCanDeactivate 确认消息
【发布时间】:2017-02-09 16:33:37
【问题描述】:

我已经实现了CanDeactivate 保护,以避免用户在上传期间离开页面并且它可以工作。问题是我的消息始终是默认消息(由于浏览器语言的原因是荷兰语),即使我自己设置消息,仍然显示相同的默认 confirm 窗口。我想写我自己的信息(实际上我有一个我想展示的模态,但首先我想看看我的简单信息)

有什么想法吗?我错过了什么吗?

这是代码

守卫。

import { Injectable, ViewContainerRef } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { DialogService } from '../services';
    
export interface PendingUpload {
   canDeactivate: () => boolean | Observable<boolean>;
}
@Injectable()
export class PendingUploadGuard implements CanDeactivate<PendingUpload> {
    constructor(
        private dialogService: DialogService,
        private viewContainerRef: ViewContainerRef
    ) { }
    
    canDeactivate(component: PendingUpload): boolean | Observable<boolean> { 
        return component.canDeactivate()
           ? true
           : confirm("Test custom message");
           //dialog I want to use later
           //this.dialogService.confirm("modal title", "modal body", this.viewContainerRef);                   
     }
 }

组件

import { Component, OnInit, HostListener } from '@angular/core';

import { Observable } from 'rxjs/Observable';

import { PendingUpload } from '../../shared/validators/pending-upload.guard';

import './../../rxjs-operators';

@Component({
    selector: 'component-selector',
    templateUrl: './html',
    styleUrls: ['./css']
})
export class ScannerUploadComponent implements OnInit, PendingUpload {
    uploading: boolean = false;

    constructor() { }

    ngOnInit() {
        this.uploading = false;
    }

    @HostListener('window:beforeunload')
    canDeactivate(): Observable<boolean> | boolean {  
        return !this.uploading;
    }
}

【问题讨论】:

  • 埃尔默在这里找到了这个问题的答案:Angular 2 - Warn user of unsaved changes before leaving page
  • 我已经看到了这个并且仍然给了我一个默认的消息文本(荷兰语)而不是我的自定义消息....我使用 Angular 文档中的特定保护并且工作但有一个像@这样的错误987654322@..我正在努力完成这项工作,但直到现在都没有成功。

标签: angular angular2-routing


【解决方案1】:

您的自定义消息只会在尝试导航到在您的 Angular 应用中的其他地方时显示(例如,从 http://localhost:4200/#/test1 导航到 http://localhost:4200/#/test2)。这是因为CanDeactivate 守卫仅在您的 Angular 应用程序中通过 Angular 路由器进行的路由更改时激活。

当导航离开您的 Angular 应用程序(例如,从 http://localhost:4200/#/test1 导航到 http://stackoverflow.com、刷新页面、关闭浏览器窗口/选项卡等)时,window @ 987654330@ 事件激活(由于@HostListener('window:beforeunload') 注释)并自行处理confirm 对话框,没有CanDeactivate 保护。在这种情况下,Chrome 和 Firefox 会显示自己的未决更改警告消息。

您可以阅读更多关于beforeunload 事件及其运作方式here 的信息。您会在“注释”部分注意到它声明了以下内容:

在 Firefox 4 及更高版本中返回的字符串不会显示给用户。相反,Firefox 会显示字符串“此页面要求您确认是否要离开 - 您输入的数据可能不会被保存。”

Chrome 遵循类似的模式,解释为 here

IE/Edge 似乎仍然提供指定自定义beforeunload 消息的能力。这方面的一个例子可以看到here

【讨论】:

  • 谢谢你的回答,炖!!我已经阅读了 beforeunload 事件,并且看到使用它时无法自定义消息。我认为我做错了什么,因为在你的回答中(来自你发布的链接)没有提到它所以......我试图做不可能的=]。至少现在我知道我走在正确的道路上。再次感谢。
  • @ElmerDantas - 没问题。我现在更新了对另一个问题的回答,以说明这种区别。很抱歉造成混乱。
猜你喜欢
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
相关资源
最近更新 更多