微信内置浏览器h5监听手机返回键

vant:

mounted(){
        //手机后退键
        let self = this;
        if (window.history && window.history.pushState) {
            $(window).on('popstate', function () {
                self.goback();
            });
            history.pushState(null, null, location.href);
            window.addEventListener('popstate', function (event) {
                history.pushState(null, null, location.href);
            });
        }
    },

 

uniapp:

mounted() {
            var a = document.getElementsByClassName('uni-page-head-hd')[0];
            a.style.display = 'none';
            //手机后退键
            let self = this;
            if (window.history && window.history.pushState) {
                window.addEventListener('popstate', function (event) {
                    uni.redirectTo({
                        url:self.$pageUrl.trainDetails + `?PlanID=${self.PlanID}`
                    },300)
                });
            }
        },

 或者:

methods:{
            onBackPress(e){
              // console.log("监听返回按钮事件",e);
              uni.redirectTo({
                  url:self.$pageUrl.trainDetails + `?PlanID=${self.PlanID}`
              },300)
              // 此处一定姚要return为true,否则页面不会返回到指定路径
              return true;
            },
}

 

posted @ 2021-10-20 14:49  石头记1  阅读(992)  评论(0编辑  收藏  举报