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

成都创新互联公司主要从事网站设计制作、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务肃州,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
梳理需求
正式开始
实践一下
回到项目中,加入beforeunload事件
通过a标签跳转(+前端路由)
- import { Prompt } from 'dva/router';
 - ....
 - render(){
 - return
 - }
 
- public handlePrompt = (location: Location) => {
 - return false;
 - };
 
- public handlePrompt = (location: Location) => {
 - return true;
 - };
 
问题来了,怎么判断是否需要跳转呢?
注意: isEqual这个方法支持比较 arrays, array buffers, booleans, date objects, error objects, maps, numbers, Object objects, regexes, sets, strings, symbols, 以及 typed arrays. Object 对象值比较自身的属性,不包括继承的和可枚举的属性。不支持函数和DOM节点比较。
实现思路讲解
- // 处理自定义离开弹窗
 - handlePrompt =(location )=>{
 - // 如果当前的保存为false,则弹窗提醒用户进行保存操作
 - if (!this.isSave) {
 - this.showModalSave(location);
 - return false;
 - }
 - return true;
 - }
 - // 展示离开的提示的弹窗
 - showModalSave = location => {
 - this.setState({
 - modalVisible: true,
 - location,
 - });
 - }
 - // 点击确认,进行页面保存操作,和保存成功后路由的跳转
 - handleSaveAuto = () => {
 - const { location } = this.state;
 - const { history } = this.props;
 - this.isSave = true;
 - this.setState({
 - modalVisible: false,
 - });
 - //进行保存操作的处理,这里可以换成自己点击确认后需要做的操作
 - this.handleSavePaper('save','exit',location.pathname)
 - }
 
- // 取消是的路由跳转
 - gotoNewUrl(url){
 - const {dispatch,history } = this.props
 - dispatch(routerRedux.push({
 - pathname:url,
 - }));
 - }
 - // 点击取消关闭弹窗
 - closeModalSave=()=>{
 - const { location } = this.state;
 - const {dispatch,history } = this.props
 - this.isSave = true;
 - this.setState({
 - modalVisible: false,
 - },()=>{
 - this.gotoNewUrl(location.pathname)
 - });
 - }
 
- title="温馨提示"
 - visible={this.state.modalVisible}
 - closable={false}
 - centered
 - onCancel={this.closeModalSave}
 - footer={null}
 - >
 即将离开当前页面,是否保存当前修改?
                网页名称:如何让用户选择是否离开当前页面?
                
                网页URL:http://www.csdahua.cn/qtweb/news14/133764.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网