Raj*_*der 5 javascript angular-routing angular
我想阻止页面刷新到处.
我试过下面的代码
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.';
}
}
}
Run Code Online (Sandbox Code Playgroud)
在尝试这样做ngOnChanges(),ngOnInit(),ngOnDestroy()甚至外面的组件类(对不起不合逻辑),但没有什么工作?
我需要Angular或JavaScript中的解决方案而不是jQuery.
谢谢.
尝试以下订阅,在页面刷新时抛出警报窗口.在尝试刷新或关闭窗口之前,请执行一些用户事件,例如单击页面.检查这里的工作版本
ngOnInit() {
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
console.log("cond");
e.returnValue = confirmationMessage; // Gecko, Trident, Chrome 34+
return confirmationMessage; // Gecko, WebKit, Chrome <34
});
}
Run Code Online (Sandbox Code Playgroud)
小智 6
@HostListener("window:beforeunload", ["$event"]) unloadHandler(event: Event) {
event.returnValue = false;
}
Run Code Online (Sandbox Code Playgroud)
Ter*_*itz -2
为此,您应该创建一个守卫。
在你的路由配置文件中:
const routes: Routes = [
{
path: '',
redirectTo: '/homePage',
pathMatch: 'full'
},
{
path: 'accueil',
component: AccueilComponent,
canDeactivate: [GuardName]
}]
Run Code Online (Sandbox Code Playgroud)
通过这样做,您将调用所选组件上的守卫
更多信息在这里
在你的守护下:
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
canDeactivate(component: CanComponentDeactivate) {
return true/false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18374 次 |
| 最近记录: |