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,并掌握了相关的注意事项和优化技巧。结合语法糖形式,可以让代码更加清晰简洁。

相关推荐
计算机学姐1 分钟前
基于SpringBoot的校园二手交易系统
java·vue.js·spring boot·后端·spring·tomcat·intellij-idea
小璐资源网2 分钟前
CSS进阶指南:深入解析选择器优先级与继承机制
前端·css
工边页字7 分钟前
为什么 RAG系统里,Embedding成本往往远低于 LLM成本,但很多公司仍然疯狂优化 Embedding?
前端·人工智能·后端
墨渊君8 分钟前
OpenClaw 上手实践: 使用 Docker 从构建到可用全流程指南
前端·agent
冰暮流星10 分钟前
javascript之回调函数
开发语言·前端·javascript
米丘14 分钟前
Rollup 打包工具
前端
We་ct15 分钟前
LeetCode 74. 搜索二维矩阵:两种高效解题思路
前端·算法·leetcode·矩阵·typescript·二分查找
moneyinto16 分钟前
Three.js 必背核心方法
前端
wuhen_n18 分钟前
Vue3 组件中的图片懒加载与渐进式加载
前端·javascript·vue.js
叫回忆18 分钟前
elpis的npm抽离与发布
前端·javascript