Vue 3 中 onUnload 和 onPageScroll 使用详解

Vue 3 中 onUnload 和 onPageScroll 使用详解

在 Vue 3 中,当我们开发微信小程序时,通常需要处理页面生命周期事件和页面滚动事件,比如页面卸载 (onUnload) 和页面滚动 (onPageScroll) 等。这些功能对优化用户体验、实现动态效果以及处理页面状态管理至关重要。

本文将详细介绍如何在 Vue 3 中使用 onUnloadonPageScroll,包括语法糖的使用方式,并附加完整代码示例。


onUnload 生命周期事件

作用

onUnload 是微信小程序的页面生命周期事件,触发时机是页面卸载(离开当前页面,或者关闭页面)时。常见的应用场景包括:

  • 清理页面数据。
  • 停止未完成的请求。
  • 释放内存占用资源(如定时器、监听器等)。

使用方式

步骤
  1. Vue 3 的 onUnload 事件可以通过组合式 API 的 onUnmounted 来实现。
  2. 如果在特定页面中处理 onUnload,需要结合小程序的 PageComponent 的生命周期绑定事件。
代码示例

以下示例展示如何在 onUnload 中清理定时器:

vue 复制代码
<template>
  <view class="container">
    <text>{{ message }}</text>
  </view>
</template>

<script setup>
import { ref, onUnmounted } from 'vue';

const message = ref('正在加载数据...');
let timer;

function startTimer() {
  timer = setInterval(() => {
    console.log('计时器运行中...');
  }, 1000);
}

// 页面卸载时清理定时器
onUnmounted(() => {
  console.log('页面卸载,清理定时器');
  if (timer) {
    clearInterval(timer);
  }
});

// 初始化逻辑
startTimer();
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
</style>

onPageScroll 页面滚动事件

作用

onPageScroll 是微信小程序提供的页面滚动事件,触发时机为用户滚动页面时。常见的应用场景包括:

  • 实现滚动加载(如无限滚动)。
  • 根据滚动位置改变 UI(如动态导航栏)。
  • 滚动动画效果。

使用方式

步骤
  1. 在 Vue 3 中,通过 onPageScroll 钩子捕获滚动事件。
  2. onPageScroll 提供滚动位置信息,通过 scrollTop 获取滚动距离。
代码示例

以下示例实现滚动动态导航栏效果:

vue 复制代码
<template>
  <view class="page">
    <view :class="['navbar', { sticky: isSticky }]">导航栏</view>
    <scroll-view scroll-y="true" class="content">
      <view v-for="item in 50" :key="item" class="item">内容 {{ item }}</view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';

const isSticky = ref(false);

// 监听滚动事件
function handleScroll({ scrollTop }) {
  isSticky.value = scrollTop > 100; // 滚动超过 100px 时固定导航栏
}

// 注册滚动事件
onMounted(() => {
  wx.onPageScroll(handleScroll);
});

// 卸载时移除滚动事件
onUnmounted(() => {
  wx.offPageScroll(handleScroll);
});
</script>

<style scoped>
.page {
  height: 100vh;
  overflow: hidden;
}
.navbar {
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #eee;
  transition: all 0.3s;
}
.navbar.sticky {
  background-color: #333;
  color: #fff;
}
.content {
  height: calc(100vh - 50px);
  overflow-y: scroll;
}
.item {
  height: 50px;
  line-height: 50px;
  text-align: center;
  border-bottom: 1px solid #ddd;
}
</style>

更多相关知识点

Vue 3 小程序支持的常见生命周期

除了 onUnloadonPageScroll,小程序开发中常用的生命周期事件包括:

  • onLoad: 页面加载时触发。
  • onShow: 页面显示时触发。
  • onHide: 页面隐藏时触发。
  • onReady: 页面初次渲染完成时触发。

事件绑定的注意事项

  1. 性能优化
    对于频繁触发的事件(如 onPageScroll),建议对事件进行节流或防抖处理。
  2. 清理资源
    onUnload 或其他生命周期结束时,应清理占用的资源(如事件监听器、定时器等),以避免内存泄漏。

总结

通过本文的学习,我们了解了如何在 Vue 3 开发的微信小程序中使用 onUnloadonPageScroll,并掌握了相关的注意事项和优化技巧。结合语法糖形式,可以让代码更加清晰简洁。

相关推荐
程序员黑豆4 小时前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
To_OC4 小时前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
GreenTea5 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
浮生望5 小时前
前端API工程化:用 Mock 数据与 Axios 配置实现独立于后端的并行开发
前端
万少5 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端
波波0075 小时前
C# 15 重磅新特性: 带标签 break 与 continue:重新定义嵌套循环控制流
服务器·前端·c#
Brown.alexis6 小时前
es6知识点1-自备使用
前端·ecmascript·es6
小爬的老粉丝7 小时前
JavaScript 纯前端预览 WPS:先识别容器,再路由解析器
前端·javascript·wps
计算机魔术师8 小时前
新兴多智能体系统的模式与问题
前端
用户059540174468 小时前
AI Agent 上下文污染踩坑实录:用 pytest + Redis 揪出 3 类记忆串号 bug,排查 6 小时
前端·css