onbeforeunload
,MDN的說明是:當瀏覽器窗口關閉或者刷新時,會觸發beforeunload事件。當前頁面不會直接關閉,能夠點擊肯定按鈕關閉或刷新,也能夠取消關閉或刷新。
beforeunload
事件`<script type="text/javascript"> window.onbeforeunload = function () { return "Leave this page?"; } </script>`
"xxx.ooo.com"
會出現
`import { Prompt } from 'dva/router'; .... render(){ return <Prompt message={this.handlePrompt} /> }`
`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節點比較。javascript
❞html
`// 處理自定義離開彈窗 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) }); }`
`<Prompt message={this.handlePrompt}/> <Modal title="舒適提示" visible={this.state.modalVisible} closable={false} centered onCancel={this.closeModalSave} footer={null} > <p>即將離開當前頁面,是否保存當前修改?</p> <div style={{textAlign:'right'}}> <Button type='primary' onClick={this.handleSaveAuto}>保存</Button> <Button style={{marginLeft:'20px'}} onClick={this.closeModalSave}>取消</Button> </div> </Modal>`
在看
,關注一波公衆號:[前端巔峯
]前端生活社區
:https://qianduan.life