react h5 下默认操作安卓 微信 回退默认不操作拦截失败,回退拦截

 
  if (UserAgent.app.weixin && UserAgent.env.android) {
      //安卓回退弹窗兼容
      try {
        window.tbs_bridge.nativeExec('network', 'type', 0, null);
      } catch (e) {
        console.error('weixin network', e);
      }
    }

 回退拦截方法didmout中调用组件方法
componentDidMount(){
     window.addEventListener('popstate', this.window_back)  
}

  window_back = () => {
    const insurance_success_age = utils.store.session_get('insurance_success_age')
    const herf = window.location.pathname //获取的是目标页地址
    const search = window.location.search //获取的是目标页地址
  //showBack= 后边是页面路由后的参数showBack if (herf === '/insurance/success_age_hegui' && search.indexOf('showBack=receive') !== -1) { if (insurance_success_age[1]) { //已存在代表已弹窗过 return } else { insurance_success_age[1] = 1 utils.store.session_set('insurance_success_age', insurance_success_age) //进行想要的回退拦截处理,比如增加弹窗 } } }

  另外一种更优方案:

   if (window.history && window.history.pushState) {
      history.pushState(null, null, document.URL);
      window.addEventListener('popstate', this.fun,false);//false阻止默认事件
    }
  fun = () => {
    // if (this.state.num == 1) {
         //  num控制第几次弹窗
      console.log("监听到了");
      // this.setState({
      //   num: 0
      // })
      history.pushState(null, null, document.URL);    //有num不加这一行  禁止回退
    // }
  }
 
 
 
 
posted @ 2021-06-25 17:54  红苹果学园  阅读(1181)  评论(0编辑  收藏  举报