【发布时间】:2017-11-16 13:43:28
【问题描述】:
我想防止到处刷新页面。
我试过下面的代码
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CommonServices } from '../services/common.service';
@Component({
selector: 'app-review-prescription',
templateUrl: './review-prescription.component.html',
styleUrls: ['../../assets/css/prescriptionView.css'],
providers:[
CommonServices
]
})
export class ReviewPrescriptionComponent implements OnInit {
constructor(
private commonServices:CommonServices,
private router:Router
){}
ngOnInit(){
window.onbeforeunload = function(event) {
return 'By refreshing this page you may lost all data.';
}
}
}
在ngOnChanges()、ngOnInit()、ngOnDestroy() 上试过这个,甚至在组件类之外(抱歉不合逻辑)但没有任何效果?
我需要 Angular 或 JavaScript 而不是 jQuery 的解决方案。
谢谢。
【问题讨论】:
-
你能多加一点你的代码吗?
-
您无法阻止页面刷新。浏览器用户总是可以按 F5。 “onbeforeunload”方法可以让你警告用户,但你不能阻止它。
-
@Pointy 你是对的,但即使警告也不起作用。问题是
window.onbeforeunload不工作:( -
“不工作”是什么意思?是否报告了错误?您是否从浏览器中获取对话框?
标签: javascript angular angular-routing