npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

uni-lifecycle

v1.0.2

Published

为 uni-app 提供页面生命周期钩子注册与参数获取的工具

Readme

uni-lifecycle

为 uni-app 提供的页面生命周期钩子注册与参数获取工具,解决在 NPM 包/插件中无法直接导入 @dcloudio/uni-app 钩子的问题,让插件开发者能够安全、便捷地注册页面生命周期回调,并优雅地获取生命周期参数。

特性

  • 插件友好 – 可在任何 NPM 包内注册页面生命周期钩子,无构建报错
  • 双模式 API – 支持注册回调 Lifecycle.onLoad(fn) 和直接获取参数 await Lifecycle.onLoad.getParams()
  • 自动参数解包 – 单参数自动返回该值,多参数返回数组(保护未来兼容性)
  • 页面实例隔离 – 每个页面的钩子独立存储,页面销毁后自动回收
  • 保留原始钩子 – 包装用户钩子时不破坏页面原有生命周期逻辑
  • TypeScript 支持 – 完整的类型定义

适用场景

  • 开发 uni-app 插件(路由、埋点、权限、UI 库等),需要在页面生命周期中注入逻辑
  • 希望在应用中全局管理页面生命周期,避免在每个页面手动编写重复代码
  • 需要异步等待 onLoad 参数(例如路由传参自动注入)

安装

pnpm add uni-lifecycle

快速上手

注册生命周期回调

import { Lifecycle } from 'uni-lifecycle';

// 页面加载时执行
Lifecycle.onLoad((query) => {
  console.log('页面参数', query);
});

// 页面显示时执行(每次显示都会触发)
Lifecycle.onShow(() => {
  console.log('页面显示了');
});

获取生命周期参数

// 等待 onLoad 参数(Promise 会在当前页面的 onLoad 触发后 resolve)
const query = await Lifecycle.onLoad.getParams();
console.log(query); // { id: 123, ... }

// 获取 onShow 参数(onShow 无参数,返回 undefined)
const showArg = await Lifecycle.onShow.getParams();

在插件中使用(典型场景)

// 你的插件内部
import { Lifecycle } from 'uni-lifecycle';

export function initTracker() {
  // 自动埋点页面访问
  Lifecycle.onLoad(() => {
    reportPageView();
  });
}

与路由库配合(自动注入参数)

// 路由库内部实现 useQuery
import { Lifecycle } from 'uni-lifecycle';

export async function useQuery(key?: string) {
  const query = await Lifecycle.onLoad.getParams();
  return key ? query?.[key] : query;
}

API 参考

Lifecycle

所有页面生命周期方法都支持两种调用形式:

  • Lifecycle.onLoad(fn: Function): void – 注册回调
  • Lifecycle.onLoad.getParams(): Promise<any> – 等待并获取参数

支持的生命周期列表(与 uni-app 页面生命周期一致):

  • onLoad
  • onShow
  • onHide
  • onReady
  • onResize
  • onRouteDone
  • onUnload

参数返回值规则

  • 如果生命周期回调只有一个参数(如 onLoad(query)),getParams() 直接返回该参数对象。
  • 如果生命周期回调无参数(如 onShow()),getParams() 返回 undefined
  • 如果将来出现多参数生命周期,getParams() 返回完整数组。

注意事项

  1. 调用时机 – 注册钩子或调用 getParams 时,确保页面栈中存在当前页面(即已在页面上下文中)。通常在页面的 setuponLoad 之前调用即可,getParams 内部会等待直到生命周期触发。
  2. 页面切换 – getParams 基于调用时刻的当前页面等待,不会受到后续页面跳转的影响。
  3. 内存管理 – 每个页面的钩子相互隔离,页面卸载时自动回收,无需手动清理。
  4. TypeScript – 包自带类型定义,无需额外安装 @types