美文网首页React
react 浏览器刷新/关闭 拦截

react 浏览器刷新/关闭 拦截

作者: Sasoli | 来源:发表于2018-09-07 15:21 被阅读0次
  componentWillMount () {
    // 拦截判断是否离开当前页面
    window.addEventListener('beforeunload', this.beforeunload);
  }
  componentWillUnmount () {
    // 销毁拦截判断是否离开当前页面
    window.removeEventListener('beforeunload', this.beforeunload);
  }
  beforeunload (e) {
    let confirmationMessage = '你确定离开此页面吗?';
    (e || window.event).returnValue = confirmationMessage;
    return confirmationMessage;
  }

相关文章

网友评论

    本文标题:react 浏览器刷新/关闭 拦截

    本文链接:https://www.haomeiwen.com/subject/aqycgftx.html