如何实现Vue中监听页面刷新和关闭的功能?

2026-04-09 23:541阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计519个文字,预计阅读时间需要3分钟。

如何实现Vue中监听页面刷新和关闭的功能?

我在项目开发过程中,遇到一个需求,当离开(跳转或关闭)购物车页面或刷新购物车页面时,向服务器提交一次购物车商品数量的变更。将这一步操作放入`beforeDestroy`钩子函数中。

我在做项目的时候,有一个需求,在离开(跳转或者关闭)购物车页面或者刷新购物车页面的时候向服务器提交一次购物车商品数量的变化。

将提交的一步操作放到 beforeDestroy 钩子函数中。

beforeDestroy() { console.log('销毁组件') this.finalCart()},

但是发现 beforeDestroy 只能监听到页面间的跳转,无法监听到页面刷新和关闭标签页。

所以还是要借助onbeforeunload 事件。

顺便复习了一下 JavaScript 中的一些加载,卸载事件:

  • 页面加载时只执行onload 事件。
  • 页面关闭时,先onbeforeunload 事件,再onunload 事件。
  • 页面刷新时先执行onbeforeunload 事件,然后onunload 事件,最后onload 事件。

Vue中监听页面刷新和关闭

1. 在methods中定义事件方法:

methods: { beforeunloadFn(e) { console.log('刷新或关闭') // ... } }

2. 在created 或者 mounted 生命周期钩子中绑定事件

created() { window.addEventListener('beforeunload', e => this.beforeunloadFn(e)) }

3. 在 destroyed 钩子卸载事件

destroyed() { window.removeEventListener('beforeunload', e => this.beforeunloadFn(e)) }

测试了页面刷洗和关闭都能监听到。

回到我自己的项目,可以使用onbeforeunload 事件和beforeDestroy 钩子函数结合:

created() { this.initCart() window.addEventListener('beforeunload', this.updateHandler) }, beforeDestroy() { this.finalCart() // 提交购物车的异步操作}, destroyed() { window.removeEventListener('beforeunload', this.updateHandler)}, methods: { updateHandler() { this.finalCart() }, finalCart() { // ... } }

总结

以上所述是小编给大家介绍的Vue监听页面刷新和关闭功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

如何实现Vue中监听页面刷新和关闭的功能?

本文共计519个文字,预计阅读时间需要3分钟。

如何实现Vue中监听页面刷新和关闭的功能?

我在项目开发过程中,遇到一个需求,当离开(跳转或关闭)购物车页面或刷新购物车页面时,向服务器提交一次购物车商品数量的变更。将这一步操作放入`beforeDestroy`钩子函数中。

我在做项目的时候,有一个需求,在离开(跳转或者关闭)购物车页面或者刷新购物车页面的时候向服务器提交一次购物车商品数量的变化。

将提交的一步操作放到 beforeDestroy 钩子函数中。

beforeDestroy() { console.log('销毁组件') this.finalCart()},

但是发现 beforeDestroy 只能监听到页面间的跳转,无法监听到页面刷新和关闭标签页。

所以还是要借助onbeforeunload 事件。

顺便复习了一下 JavaScript 中的一些加载,卸载事件:

  • 页面加载时只执行onload 事件。
  • 页面关闭时,先onbeforeunload 事件,再onunload 事件。
  • 页面刷新时先执行onbeforeunload 事件,然后onunload 事件,最后onload 事件。

Vue中监听页面刷新和关闭

1. 在methods中定义事件方法:

methods: { beforeunloadFn(e) { console.log('刷新或关闭') // ... } }

2. 在created 或者 mounted 生命周期钩子中绑定事件

created() { window.addEventListener('beforeunload', e => this.beforeunloadFn(e)) }

3. 在 destroyed 钩子卸载事件

destroyed() { window.removeEventListener('beforeunload', e => this.beforeunloadFn(e)) }

测试了页面刷洗和关闭都能监听到。

回到我自己的项目,可以使用onbeforeunload 事件和beforeDestroy 钩子函数结合:

created() { this.initCart() window.addEventListener('beforeunload', this.updateHandler) }, beforeDestroy() { this.finalCart() // 提交购物车的异步操作}, destroyed() { window.removeEventListener('beforeunload', this.updateHandler)}, methods: { updateHandler() { this.finalCart() }, finalCart() { // ... } }

总结

以上所述是小编给大家介绍的Vue监听页面刷新和关闭功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

如何实现Vue中监听页面刷新和关闭的功能?