F7+vue 物理返回键监听使用

以前使用的是纯F7,这个项目加了Vue进去,但碰到了一个问题,就是这样监听不到安卓物理键的返回,它是点击返回,直接推出程序,这个坑有点深,查了不少资料也问了不少人,最后在网上看到了别人的写的,自己也改了一点,最后可以返回,但是不能直接退出,坑还是没有爬完:上代码:

    var XBack = {};
    (function(XBack) {
        XBack.STATE = 'x - back';
        XBack.element;

        XBack.onPopState = function(event) {
            event.state === XBack.STATE && XBack.fire();
            XBack.record(XBack.STATE); //初始化事件时,push一下  
             
        };

        XBack.record = function(state) {
            history.pushState(state, null, location.href);

        };

        XBack.fire = function() {
            var event = document.createEvent('Events');
            event.initEvent(XBack.STATE, false, false);
            XBack.element.dispatchEvent(event);
              
        };

        XBack.listen = function(listener) {
            XBack.element.addEventListener(XBack.STATE, listener, false);
        };

        XBack.init = function() {
            XBack.element = document.createElement('span');
            window.addEventListener('popstate', XBack.onPopState);
            XBack.record(XBack.STATE);
            
        };

    })(XBack); // 引入这段js文件  
    XBack.init();
   mounted() {
      var $_this = this;
            XBack.listen(function() {
                $_this.$router.back()
            }); 
  },

这段代码放在入口页就可以了

 

转载于:https://www.cnblogs.com/MyIsLu/p/7607728.html

你可能感兴趣的:(F7+vue 物理返回键监听使用)