{"id":13726935,"url":"https://github.com/yangger6/mini-lifecycle-interceptor","last_synced_at":"2025-05-07T22:30:39.142Z","repository":{"id":57297601,"uuid":"379791275","full_name":"yangger6/mini-lifecycle-interceptor","owner":"yangger6","description":"Mini Program lifeCycle interceptor.小程序生命周期拦截器","archived":false,"fork":false,"pushed_at":"2021-12-10T09:59:40.000Z","size":84,"stargazers_count":19,"open_issues_count":3,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-21T18:19:59.578Z","etag":null,"topics":["interceptor","mini-program","wechat-mini-program"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yangger6.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-24T03:22:06.000Z","updated_at":"2025-02-19T09:04:10.000Z","dependencies_parsed_at":"2022-09-01T06:40:11.526Z","dependency_job_id":null,"html_url":"https://github.com/yangger6/mini-lifecycle-interceptor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangger6%2Fmini-lifecycle-interceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangger6%2Fmini-lifecycle-interceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangger6%2Fmini-lifecycle-interceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yangger6%2Fmini-lifecycle-interceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yangger6","download_url":"https://codeload.github.com/yangger6/mini-lifecycle-interceptor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252965103,"owners_count":21832822,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interceptor","mini-program","wechat-mini-program"],"created_at":"2024-08-03T01:03:32.118Z","updated_at":"2025-05-07T22:30:38.750Z","avatar_url":"https://github.com/yangger6.png","language":"TypeScript","readme":"# mini-lifecycle-interceptor\n\n[![en](https://img.shields.io/badge/en-English-blue)](https://github.com/yangger6/mini-lifecycle-interceptor/blob/main/README_EN.md)\n\n小程序生命周期方法拦截器\n\n\n## 特性\n\n- [x]  拦截Page\n- [x]  拦截Component\n- [x]  支持Promise\n- [ ]  支持过滤\n- [ ]  支持排除\n\n## 运行平台支持\n\n- [x]  支持微信小程序\n- [x]  支持Taro\n- [x]  支持支付宝小程序\n\n不同的小程序平台的拦截器方法的支持程度如下: \n\n| 方法名         | 容器类型        |描述                                                                         | 微信小程序        | 支付宝小程序    |\n| ------------- | ------------- |---------------------------------------------------------------------------- | --------------- | ------------- |\n| onLoad        | `Page`        | 页面加载时触发。一个页面只会调用一次，可以在 onLoad 的参数中获取打开当前页面路径中的参数。 | ✔️           \t| ✔️            |\n| onShow        | `Page`        | 页面显示/切入前台时触发                                                         | ✔️           \t| ✔️            |\n| onReady       | `Page`        | 页面初次渲染完成时触发                                                          | ✔️           \t| ✔️            |\n| onHide        | `Page`        | 页面隐藏/切入后台时触发                                                         | ✔️           \t| ✔️            |\n| onUnload      | `Page`        | 页面卸载时触发                                                                 | ✔️           \t| ✔️            |\n| created       | `Component`   | 在组件实例进入页面节点树时执行                                                    | ✔️           \t| ✔️            |\n| attached      | `Component`   | 在组件实例进入页面节点树时执行                                                    | ✔️           \t| ✔️            |\n| ready         | `Component`   | 在组件在视图层布局完成后执行                                                     | ✔️           \t| ✔️            |\n| detached      | `Component`   | 在组件实例被从页面节点树移除时执行                                                | ✔️           \t| ✔️            |\n| moved         | `Component`   | 在组件实例被移动到节点树另一个位置时执行                                           | ✔️           \t| ✘             |\n| update        | `Component`   | 组件更新完毕时触发                                                             | ✘           \t| ✔️            |\n| error         | `Component`   | 每当组件方法抛出错误时执行                                                       | ✔️           \t| ✔️            |\n\n## 安装\n\n使用 npm:\n\n```bash\n$ npm install mini-lifecycle-interceptor --save\n```\n\n使用 yarn:\n\n```bash\n$ yarn add mini-lifecycle-interceptor\n```\n\n## 示例\n\n使用Taro:\n\n```typescript\n\nimport { useEffect } from 'react'\nimport miniLifeCycle from 'mini-lifecycle-interceptor'\nminiLifeCycle.create(process.env.TARO_ENV) // weapp | alipay, default: weapp\n\nfunction App(props: any) {\n    const delay = (t, prefix) =\u003e\n        new Promise((res) =\u003e\n            setTimeout(() =\u003e {\n                console.log(`${prefix}: delay ${t}ms`)\n                res()\n            }, t)\n        )\n    useEffect(() =\u003e {\n        miniLifeCycle.interceptors.onLoad.use(async function (options, next) {\n            console.log('before onLoad', options)\n            await delay(1000, 'step1')\n            next()\n        })\n        miniLifeCycle.interceptors.onLoad.use([\n            async function (options, next) {\n                console.log('after onLoad', options)\n                await delay(1000, 'step2')\n                next()\n            },\n            async function (options, next) {\n                console.log('after onLoad', options)\n                await delay(1000, 'step3')\n                next()\n            }\n        ])\n        miniLifeCycle.interceptors.onLoad.useAfter([\n            async function (options, next) {\n                console.log('after onLoad', options)\n                await delay(1000, 'step4')\n                next()\n            },\n            async function (options, next) {\n                console.log('after onLoad', options)\n                await delay(1000, 'step5')\n                next()\n            }\n        ])\n    }, [])\n}\n// Console\n// step1: delay 1000ms\n// step2: delay 1000ms\n// step3: delay 1000ms\n// step origin fn onLoad\n// step4: delay 1000ms\n// step5: delay 1000ms\n```\n\n使用微信小程序:\n\n[在开发者工具中预览效果](https://developers.weixin.qq.com/s/owOFbrmR79v1)\n\n## API\n\n创建方法可以用来修改 `mini-lifecycle-interceptor`的配置.\n\nminiLifeCycle.create(env, config)\n\n```typescript\n// 微信 weapp 支付宝 alipay\nminiLifeCycle.create('weapp', { // or alipay\n    // TODO\n})\n```\n\n## 拦截器方法\n\n为方便起见，为所有支持的拦截器方法提供了别名\n\n### 页面生命周期方法拦截器\n\nminiLifeCycle.interceptors.onLoad.use // or miniLifeCycle.interceptors.onLoad.useAfter\n\nminiLifeCycle.interceptors.onShow.use // or miniLifeCycle.interceptors.onShow.useAfter\n\nminiLifeCycle.interceptors.onHide.use // or miniLifeCycle.interceptors.onHide.useAfter\n\nminiLifeCycle.interceptors.onReady.use // or miniLifeCycle.interceptors.onReady.useAfter\n\nminiLifeCycle.interceptors.onUnload.use // or miniLifeCycle.interceptors.onUnload.useAfter\n\n### 自定义组件生命周期方法拦截器\n\nminiLifeCycle.interceptors.created.use // or miniLifeCycle.interceptors.created.useAfter\n\nminiLifeCycle.interceptors.attached.use // or miniLifeCycle.interceptors.attached.useAfter\n\nminiLifeCycle.interceptors.ready.use // or miniLifeCycle.interceptors.ready.useAfter\n\nminiLifeCycle.interceptors.detached.use // or miniLifeCycle.interceptors.detached.useAfter\n\nminiLifeCycle.interceptors.moved.use // or miniLifeCycle.interceptors.moved.useAfter\n\nminiLifeCycle.interceptors.error.use // or miniLifeCycle.interceptors.error.useAfter\n\n## 许可证\n\n[MIT](LICENSE)\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyangger6%2Fmini-lifecycle-interceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyangger6%2Fmini-lifecycle-interceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyangger6%2Fmini-lifecycle-interceptor/lists"}