【发布时间】: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@..我正在努力完成这项工作,但直到现在都没有成功。