vue监听浏览器窗口关闭

vue监听浏览器窗口关闭

需求:后端需要在用户退出或者关闭页面的时候调用一个接口
相关技术点
页面关闭和刷新时, 触发 beforeunload事件
当文档或一个子资源正在被卸载时, 触发 unload事件,unload在beforeunload之后被触发

参考了网上大家分享的一些方法后,根据自身实际情况调整后可用~
参考网址:https://blog.youkuaiyun.com/robin90814/article/details/90603369

第一步 data里放好要用到的变量

  data(){
    return {
      _beforeUnload_time:'',
      _gap_time:''
    }
  },

第二步 监听

  mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.addEventListener('unload', e => this.unloadHandler(e))
  },
  destroyed() {
    window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.removeEventListener('unload', e => this.unloadHandler(e))
  },

第三步 必须使用同步请求
此处用的是post方法,默认的contentType接口报错了。
设置好contentType后,报错‘unexpected token o in JSON at position ’ ,后端拿不到值。将参数先用JSON.stringify()转成 JSON字符串就好了。

  methods: {
    beforeunloadHandler(e){
      this._beforeUnload_time = new Date().getTime();
      //e.returnValue = '关闭提示'; 弹窗
    },
    unloadHandler(e){
      this._gap_time = new Date().getTime() - this._beforeUnload_time;
      //判断是窗口关闭还是刷新
      if (this._gap_time <= 5) {
        let datas = {
          objCode:null,
          opertion:'退出登录ajax',
          param:null,
          path:null,,}
        datas =  JSON.stringify(datas)
        $.ajax({
          url: this.$apiUrl.user.oper,
          type: 'post',
          contentType:'application/json',
          data:datas,
          dataType: "json",
          async: false, //或false,是否异步
        })
      }
    },
  },
Vue.js 是一种用于构建用户界面的渐进式前端框架,它允许开发者通过声明式的绑定将数据与 UI 进行关联。对于监听浏览器窗口的操作,Vue 提供了多种方法来进行响应式处理。 ### 监听浏览器窗口操作 在 Vue 中,你可以直接在组件的 `mounted` 或者 `created` 生命周期钩子中监听一些全局事件,如滚动、大小改变等。下面是一些常见的浏览器窗口操作及其在 Vue 中的实现示例: #### 监听滚动事件 ```javascript export default { mounted() { window.addEventListener('scroll', this.onScroll); }, beforeDestroy() { window.removeEventListener('scroll', this.onScroll); }, methods: { onScroll(event) { // 当页面滚动时执行的操作 console.log('Page is scrolled!'); // 可以在这里添加更多的处理逻辑 } } } ``` #### 监听窗口尺寸变化 使用 `resize-event` 插件可以更容易地监听窗口尺寸变化: 1. 首先安装插件: ```bash npm install resize-event ``` 2. 引入并使用: ```javascript import ResizeEvent from 'resize-event'; export default { data() { return { width: null, height: null }; }, mounted() { new ResizeEvent('.my-component').subscribe(({width, height}) => { this.width = width; this.height = height; }); } } ``` ### 使用 Vue 的自定义指令监听特定元素的尺寸变化 ```javascript // 定义自定义指令 Vue.directive('on-resize', { inserted: function (el, binding, vnode) { const { value } = binding; el.addEventListener('resize', value); // 清理函数,当组件销毁时移除事件监听 vnode.context.$once('hook:beforeDestroy', () => { el.removeEventListener('resize', value); }); } }); // 在组件中使用 export default { directives: { 'on-resize': Vue.directive('on-resize') }, components: {}, template: ` <div @on-resize="handleResize"> <!-- 其他内容 --> </div> `, methods: { handleResize() { console.log('Window size has been changed'); // 在这里处理尺寸变化的具体逻辑 } } } ``` ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值