wndow.popstate,微信浏览器返回事件, vue mixins

2019-03-28 15:39:19 浏览数 (2)

微信浏览器返回键按下跳转路由 window popstate, Vue全中 写入 mixins文件

引入
代码语言:javascript复制
main.js 全局引入
import popstate from './components/newComponent/popstate'

Vue.mixin(popstate)
使用

使用时 this.init(‘router’)

  • 此处router为 留有name属性值
  • router 为空时 执行 history.go(-1)
mixin
代码语言:javascript复制
export default {
  data () {
    return {
      next : false,
      popStateRouter : ''
    }
  },
  methods : {
    resetRouterName () {
      let that = this;
      that.popStateRouter = ''
    },
    popStateInit ( router = '' ) {
      let that = this;
      that.popStateRouter = router
      try {
        if ( window.__wxjs_is_wkwebview === true ) {
          //function ...  WKWebview ios
          window.addEventListener('pageshow', ( e ) => {
            console.info('执行 ios onpageshow 进入页面')
            e.preventDefault()
            setTimeout(() => {
              that.next = true
            }, 1000);
            that.popStateInitIos()
          }, false)
        } else if ( window.__wxjs_is_wkwebview === false || window.__wxjs_is_wkwebview === undefined ) {
          //function ...  UIWebview
          that.popStateInitAndroid();
        }
      } catch ( e ) {
      }
    },
    popStateInitIos () {
      let that = this;
      console.info('ios', that.popStateRouter)
      
      window.addEventListener('popstate', function ( e ) {
        console.info('进入popstate监听 ios')
        e.preventDefault()
        if ( that.next ) {
          console.info(`通过next 执行 ${that.popStateRouter}`)
          that.$router.push({ name : that.popStateRouter })
          that.next = false;
          that.resetRouterName()
          return false
        }
        // history.go(-1)
      }, false)
    },
    popStateInitAndroid () {
      let that = this;
      console.info('andriod', that.popStateRouter)
      
      window.addEventListener('popstate', function ( e ) {
        console.info('进入popstate监听 andriod')
        e.preventDefault()
        if ( that.popStateRouter ) {
          console.info(`跳转${that.popStateRouter}`)
          that.$router.push({ name : that.popStateRouter })
          that.resetRouterName()
          return false
        }
        // history.go(-1)
      }, false)
    }
  }
}

0 人点赞