13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

如何让用户选择是否离开当前页面?

为什么要让用户选择是否离开页面

成都创新互联公司主要从事网站设计制作、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务肃州,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

梳理需求

正式开始

实践一下

回到项目中,加入beforeunload事件

 
 
 
 
  1.   
  2.        window.onbeforeunload = function () {  
  3.            return "Leave this page?";  
  4.        }  
  5.     

通过a标签跳转(+前端路由)

 
 
 
 
  1. import { Prompt } from 'dva/router';  
  2. ....  
  3. render(){  
  4.   return   
 
 
 
 
  1. public handlePrompt = (location: Location) => {  
  2.        return false;  
  3.    }; 

 
 
 
 
  1. public handlePrompt = (location: Location) => {  
  2.       return true;  
  3.   }; 

问题来了,怎么判断是否需要跳转呢?

        注意: isEqual这个方法支持比较 arrays, array buffers, booleans, date objects, error objects, maps, numbers, Object objects, regexes, sets, strings, symbols, 以及 typed arrays. Object 对象值比较自身的属性,不包括继承的和可枚举的属性。不支持函数和DOM节点比较。   

实现思路讲解

 
 
 
 
  1. // 处理自定义离开弹窗  
  2.   handlePrompt =(location )=>{  
  3.     // 如果当前的保存为false,则弹窗提醒用户进行保存操作  
  4.     if (!this.isSave) {  
  5.       this.showModalSave(location);  
  6.       return false;  
  7.     }  
  8.     return true;  
  9.   }  
  10.   // 展示离开的提示的弹窗  
  11.   showModalSave = location => {  
  12.     this.setState({  
  13.       modalVisible: true,  
  14.       location,  
  15.     });  
  16.   } 
  17.    // 点击确认,进行页面保存操作,和保存成功后路由的跳转  
  18.   handleSaveAuto = () => {  
  19.     const { location } = this.state;  
  20.     const { history } = this.props;  
  21.     this.isSave = true;  
  22.     this.setState({  
  23.       modalVisible: false,  
  24.     });  
  25.     //进行保存操作的处理,这里可以换成自己点击确认后需要做的操作  
  26.     this.handleSavePaper('save','exit',location.pathname)  
  27.   } 
 
 
 
 
  1. // 取消是的路由跳转  
  2.   gotoNewUrl(url){  
  3.     const {dispatch,history } = this.props  
  4.     dispatch(routerRedux.push({  
  5.       pathname:url,  
  6.     }));  
  7.   }  
  8.   // 点击取消关闭弹窗  
  9.   closeModalSave=()=>{  
  10.     const { location } = this.state;  
  11.     const {dispatch,history } = this.props  
  12.     this.isSave = true;  
  13.     this.setState({  
  14.       modalVisible: false,  
  15.     },()=>{  
  16.       this.gotoNewUrl(location.pathname)  
  17.     });  
  18.   } 
 
 
 
 
  1.   
  2.     title="温馨提示"  
  3.     visible={this.state.modalVisible}  
  4.     closable={false}  
  5.     centered  
  6.     onCancel={this.closeModalSave}  
  7.     footer={null}  
  8. >  
  9.   

    即将离开当前页面,是否保存当前修改?

     
  10.     
  11.     保存  
  12.     取消  
  13.   
  
  •  

  • 文章题目:如何让用户选择是否离开当前页面?
    本文地址:http://cdbrznjsb.com/article/dhdeccj.html

    其他资讯

    让你的专属顾问为你服务