{"id":29102377,"url":"https://github.com/wechat-miniprogram/chaining-api-polyfill","last_synced_at":"2026-02-19T11:03:24.134Z","repository":{"id":267801532,"uuid":"898867329","full_name":"wechat-miniprogram/chaining-api-polyfill","owner":"wechat-miniprogram","description":"让小程序中的非 glass-easel 页面和组件也能使用 Chaining API","archived":false,"fork":false,"pushed_at":"2025-08-27T11:42:21.000Z","size":535,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-09-28T08:22:53.552Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wechat-miniprogram.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-12-05T07:28:08.000Z","updated_at":"2025-08-27T11:42:11.000Z","dependencies_parsed_at":"2025-08-25T11:18:39.135Z","dependency_job_id":"7419998f-bee9-48a1-8860-be851ccdd478","html_url":"https://github.com/wechat-miniprogram/chaining-api-polyfill","commit_stats":null,"previous_names":["wechat-miniprogram/chaining-api-polyfill"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/wechat-miniprogram/chaining-api-polyfill","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fchaining-api-polyfill","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fchaining-api-polyfill/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fchaining-api-polyfill/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fchaining-api-polyfill/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wechat-miniprogram","download_url":"https://codeload.github.com/wechat-miniprogram/chaining-api-polyfill/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fchaining-api-polyfill/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29611002,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T10:52:55.328Z","status":"ssl_error","status_checked_at":"2026-02-19T10:52:26.323Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2025-06-28T21:44:59.159Z","updated_at":"2026-02-19T11:03:24.129Z","avatar_url":"https://github.com/wechat-miniprogram.png","language":"TypeScript","readme":"# Chaining API Polyfill\n\n为在非 glass-easel 组件框架下运行的小程序提供 Chaining API 支持。\n\n\n## 为什么要使用 Chaining API ？\n\n小程序使用传统的 `Page` 或 `Component` 构造器时，在复杂情况下会很不好用。\n\n例如，如果页面或组件有个复杂的私有变量，只能选择将它写入到 `this` 的某个字段上，或者 setData 到 `this.data` 上。无论哪种处理方式，对 TypeScript 都不太友好，而且可能带来较大的性能开销。\n\n为了解决这类问题，小程序的 [glass-easel 组件框架](https://github.com/wechat-miniprogram/glass-easel)提供了 [Chaining API](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/glass-easel/chaining-api.html) 。其中最重要的是提供了 `init` 函数。使用 Chaining API 可以编写出更易于维护、对 TypeScript 更友好的组件代码。\n\n然而，在未激活 glass-easel 组件框架时，或在旧版本的小程序基础库中， Chaining API 不被支持。这个 polyfill 可以在这些时候补上 Chaining API 支持。\n\n\n## 基础用法\n\n基本示例可参考 [这个代码片段](https://developers.weixin.qq.com/s/IFJUZtmU7qWW) 。\n\n首先在 npm 中引入：\n\n```shell\nnpm install --save miniprogram-chaining-api-polyfill\n```\n\nnpm install 之后，需要点一下小程序开发者工具菜单中的“工具”——“构建 npm ”。\n\n另外，需要确认 `tsconfig` 中的 `noImplicitThis` 选项已经打开。\n\n在想要使用 Chaining API 的页面或组件文件中，引入 polyfill 过的 `Component` 构造器：\n\n```ts\nimport { Component } from 'miniprogram-chaining-api-polyfill'\n\n// 然后就可以使用 Chaining API 了\nComponent()\n  // ...\n  .register()\n```\n\n注意：如果这个组件本身只用在 glass-easel 组件框架下，最好不要在这个组件文件中引入 polyfill 。\n\n类似地，也有：\n\n```ts\nimport { Behavior } from 'miniprogram-chaining-api-polyfill'\n```\n\n\n## 这个 Polyfill 对 Chaining API 的支持度\n\n这个 Polyfill 支持提供绝大多数 Chaining API 系列接口。但受限于旧版小程序框架，仍有少量接口无法支持。\n\n以下是不支持的接口列表及相应的修改建议。\n\n### 不支持 `.data(...)`\n\n使用 `.staticData(...)` 代替，例如：\n\n```ts\nComopnent()\n  .data(() =\u003e ({ hello: 'world' })) // 不支持\n  .register()\n```\n\n改为：\n\n```ts\nComponent()\n  .staticData({ hello: 'world' })\n  .register()\n```\n\n### `.init(...)` 中的 `observer` 需要在外部有相应定义\n\n`.init(...)` 中不直接支持 `observer` 。如需使用，需要在链式调用上预留一个空函数。例如：\n\n```ts\nComopnent()\n  .init(({ observer }) =\u003e {\n    observer('hello', () =\u003e { // 不支持\n      // do something\n    })\n  })\n  .register()\n```\n\n改为：\n\n```ts\nComopnent()\n  .observer('hello', () =\u003e {}) // 预留一个空函数作为定义\n  .init(({ observer }) =\u003e {\n    observer('hello', () =\u003e {\n      // do something\n    })\n  })\n  .register()\n```\n\n### `.init(...)` 中不支持 `relation`\n\n`.init(...)` 中不能使用 `relation` 。如需使用，需要写在链式调用上。例如：\n\n```ts\nComponent()\n  .init(({ relation }) =\u003e {\n    relation('another', { type: 'parent' }) // 不支持\n  })\n  .register()\n```\n\n改为：\n\n```ts\nComponent()\n  .relation('another', { type: 'parent' })\n  .register()\n```\n\n### 关于 Trait Behaviors\n\n这个 Polyfill 提供了对 trait behaviors 的支持。\n\n但是，不能使用 `this.hasBehavior(...)` 来判定 trait behaviors ，应使用 `this.traitBehavior(...)` 。例如：\n\n```ts\nconst helloTrait = Behavior.trait\u003c{ hello: () =\u003e string }\u003e()\n\nComponent()\n  .init(({ self, implement, lifetime }) =\u003e {\n    implement(helloTrait, { hello: () =\u003e 'world' } })\n    lifetime('attached', () =\u003e {\n      const hello = self.traitBehavior(helloTrait).hello()\n      console.info(hello)\n    })\n  })\n```\n\n### 其他不支持的细节\n\n* 链式调用上的 `lifetime` `pageLifetime` `observer` 不支持设置 `once` 参数。\n* 不支持 `.chainingFilter(...)` 。\n\n\n## LICENSE\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwechat-miniprogram%2Fchaining-api-polyfill","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwechat-miniprogram%2Fchaining-api-polyfill","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwechat-miniprogram%2Fchaining-api-polyfill/lists"}