vue项目中监听手机物理返回键的实现
前端  /  管理员 发布于 3年前   543
背景:项目中有一个场景要监听android手机物理返回键,但是app和js的中间件又没提供这个事件的监听,只能百度纯js实现了 第一步: xback.js 第二步: 加载xback.js文件 自定义组件remote-script可以参考我另外的一篇文章:/article/178777.htm 第三步: 监听返回键事件 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。;!function(pkg, undefined){ var STATE = 'x-back'; var element; var onPopState = function(event){ event.state === STATE && fire(); } var record = function(state){ history.pushState(state, null, location.href); } var fire = function(){ var event = document.createEvent('Events'); event.initEvent(STATE, false, false); element.dispatchEvent(event); } var listen = function(listener){ element.addEventListener(STATE, listener, false); } ;!function(){ element = document.createElement('span'); window.addEventListener('popstate', onPopState); this.listen = listen; this.record = record(STATE); record(STATE); }.call(window[pkg] = window[pkg] || {});}('XBack');
methods: { // JavaScript监听手机物理返回键 loadXBack () { window.XBack.listen(() => { this.dialog = this.$createDialog({ type: 'confirm', content: `确定返回吗?`, confirmButton: { text: '确定', active: true, disabled: false }, cancelButton: { text: '取消', active: false, disabled: false }, onConfirm: () => { this.dialog.hide() this.close() }, onCancel: () => { this.dialog.hide() window.history.pushState('x-back', null, window.location.href) } }) this.dialog.show() }) },}
您可能感兴趣的文章:
路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..mashrdn 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 V2rayn免费节点添加上去了,youtobe无法打开网页,是怎么回事..张伟 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 3q!有用,不过免费节点隔天就要去git上复制新的导进去..博主 在
科学上网翻墙访问Google , 上外网神器佛跳墙VPN(永久免费)使用流程步骤中评论 该篇教程已不能用了,告知大家,免的老有老铁问我!..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号