我已经实现了一个CanDeactivate
后卫,以避免用户在上传时离开页面和它的作品。问题在于我的信息始终是默认信息(因为浏览器语言,因此在荷兰语中),甚至自己设置信息,仍然显示相同的默认窗口confirm
。我想我自己写的消息(其实我有我要显示一个模式,但首先我想看看我的简单的信息只是工作)CanDeactivate确认消息
任何想法可能是什么?我错过了什么吗? 在此先感谢!
这里的代码
卫队。
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;
}
}
埃尔默发现安德斯这个问题在这里:角2 - 离开页面之前警告的未保存的更改用户](http://stackoverflow.com/questions/35922071/angular-2-warn-user-of-unsaved-我已经看到了这一点,仍然改变前方的离开页/) –
给了我一个默认的消息文本(在荷兰),而不是我的自定义消息....我使用指定的保护从角文档和工作,但有一个bug像[this](https://github.com/angular/angular/issues/10321)..我很努力地完成这项工作,但直到现在还没有成功。 –