uniapp中使用h5 +开发遇到物理按键和自己设置的导航方式不一致时,可以参考以下内容

 //获取当前webview
 const webview = plus.webview.currentWebview();

 //一级页面
 const endList = ['/home','/profile'] 
 //监听物理返回按键
 plus.key.addEventListener("backbutton", function () {
   //如果是一级页面直接退出,或者你希望提醒用户也可以在这里加个弹窗
  if (endList.includes(route.path)){
      //清空原生Webview窗口加载的内容
      webview.clear()
      plus.runtime.quit()
      return
    };
    webview.canBack((e) => {
      //如果可以返回,直接返回
      if (e.canBack) return router.back()
      //否则清空原生Webview窗口加载的内容并退出app
      webview.clear()
      plus.runtime.quit();
    });

  },false);
 


/**注意:在每个页面中,不需要回跳的页面都用router.replace覆盖原先记录来保持路由历史记录
*  顺序的正确性
*/

//全局路由拦截器
router.beforeEach((to, from, next) => {

      next()
})




Logo

加入社区!打开量化的大门,首批课程上线啦!

更多推荐