{"id":25112545,"url":"https://github.com/agileago/vue3-oop","last_synced_at":"2025-05-16T16:04:56.952Z","repository":{"id":38219024,"uuid":"426082798","full_name":"agileago/vue3-oop","owner":"agileago","description":"使用类和依赖注入写vue组件","archived":false,"fork":false,"pushed_at":"2025-05-12T10:03:34.000Z","size":3955,"stargazers_count":167,"open_issues_count":1,"forks_count":16,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-12T11:24:37.927Z","etag":null,"topics":["decorators","dependency-injection","ioc","vue3"],"latest_commit_sha":null,"homepage":"https://agileago.github.io/vue3-oop/","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/agileago.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2021-11-09T03:49:56.000Z","updated_at":"2025-05-12T10:03:47.000Z","dependencies_parsed_at":"2024-06-18T22:42:57.781Z","dependency_job_id":"9e284c1e-9770-44f2-ad56-6221fb705e94","html_url":"https://github.com/agileago/vue3-oop","commit_stats":null,"previous_names":[],"tags_count":68,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agileago%2Fvue3-oop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agileago%2Fvue3-oop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agileago%2Fvue3-oop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agileago%2Fvue3-oop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agileago","download_url":"https://codeload.github.com/agileago/vue3-oop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254564120,"owners_count":22092121,"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":["decorators","dependency-injection","ioc","vue3"],"created_at":"2025-02-08T01:53:04.310Z","updated_at":"2025-05-16T16:04:56.698Z","avatar_url":"https://github.com/agileago.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vue3 oop [文档](https://agileago.github.io/vue3-oop/)\n\n类组件+自动化的依赖注入(可选) = 极致的代码体验 [DEMO](https://stackblitz.com/edit/vite-y7m4fy?file=main.tsx)\n\n\n### 前提条件\n\n需要**reflect-metadata** 的支持\n\n```shell\npnpm add @abraham/reflection injection-js \n```\n\n项目入口需要引入 `reflect-metadata`\n\n```typescript\nimport '@abraham/reflection'\n```\n\n**`tsconfig.json`** 需要增加配置:\n\n```json\n{\n  \"compilerOptions\": {\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true,\n    \"useDefineForClassFields\": false\n  } \n}\n```\n\n### 安装\n\n```shell\npnpm add vue3-oop \n```\n\n### vite配置\n\n因为esbuild不支持装饰器的metadata属性，所以需要安装 `@vue3-oop/plugin-vue-jsx` 插件使用原始ts编译\n\n### 定义组件\n\n```typescript jsx\nimport { Autobind, ComponentProps, Computed, Hook, Link, Mut, VueComponent } from 'vue3-oop'\nimport { Directive, VNodeChild, watch } from 'vue'\n\ninterface FooProps {\n  size: 'small' | 'large'\n  // 组件的slots\n  slots: {\n    item(name: string): VNodeChild\n  }\n}\n\nclass Foo extends VueComponent\u003cFooProps\u003e {\n  // vue需要的运行时属性检查\n  static defaultProps: ComponentProps\u003cFooProps\u003e = ['size']\n\n  constructor() {\n    super()\n    // watch在构造函数中初始化\n    watch(\n      () =\u003e this.count,\n      () =\u003e {\n        console.log(this.count)\n      },\n    )\n  }\n\n  // 组件自身状态\n  @Mut() count = 1\n\n  // 计算属性\n  @Computed()\n  get doubleCount() {\n    return this.count * 2\n  }\n\n  add() {\n    this.count++\n  }\n\n  // 自动绑定this\n  @Autobind()\n  remove() {\n    this.count--\n  }\n\n  // 生命周期\n  @Hook('Mounted')\n  mount() {\n    console.log('mounted')\n  }\n\n  // 对元素或组件的引用\n  @Link() element?: HTMLDivElement\n\n  render() {\n    return (\n      \u003cdiv ref=\"element\"\u003e\n        \u003cspan\u003e{this.props.size}\u003c/span\u003e\n        \u003cbutton onClick={() =\u003e this.add()}\u003e+\u003c/button\u003e\n        \u003cspan\u003e{this.count}\u003c/span\u003e\n        \u003cbutton onClick={this.remove}\u003e-\u003c/button\u003e\n        \u003cdiv\u003e{this.context.slots.item?.('aaa')}\u003c/div\u003e\n        \u003cinput type=\"text\" v-focus/\u003e\n      \u003c/div\u003e\n    )\n  }\n}\n\n```\n\n### 定义服务\n\n组件和服务的差距是缺少了render这一个表现UI的函数，其他都基本一样\n\n```typescript\nclass CountService extends VueService {\n  @Mut() count = 1\n  add() {\n    this.count++\n  }\n  remove() {\n    this.count--\n  }\n}\n```\n\n\n### 依赖注入\n\nAngular文档\n\n- [Angular 中的依赖注入](https://angular.cn/guide/dependency-injection)\n- [依赖提供者](https://angular.cn/guide/dependency-injection-providers)\n- [服务与依赖注入简介](https://angular.cn/guide/architecture-services)\n- [多级注入器](https://angular.cn/guide/hierarchical-dependency-injection)\n- [依赖注入实战](https://angular.cn/guide/dependency-injection-in-action)\n\n```typescript jsx\nimport { VueComponent, VueService } from 'vue3-oop'\nimport { Injectable } from 'injection-js'\n\n// 组件DI\n@Component({\n  providers: [CountService]\n})\nclass Bar extends VueComponent {\n  constructor(private countService: CountService) {super()}\n\n  render() {\n    return \u003cdiv\u003e{this.countService.count}\u003c/div\u003e\n  }\n}\n\n@Injectable()\nclass BarService extends VueService {\n  constructor(private countService: CountService) {super()}\n}\n```\n\n### 支持我\n\n#### 微信\n\n\u003cimg src=\"https://github.com/agileago/vue3-oop/assets/11799110/30a79e6d-6b5a-4213-a863-cee70f83d4c8\" width=\"375\" /\u003e\n\n\n#### 支付宝\n\n\u003cimg src=\"https://github.com/agileago/vue3-oop/assets/11799110/ea4e359a-0037-4c41-8e80-091547dd0bca\" width=\"375\" /\u003e\n\n### QQ交流群\n\n\u003cimg src=\"https://user-images.githubusercontent.com/11799110/163750676-784add60-422d-47ad-bf0f-e9ba6adaacda.jpeg\" width=375\u003e\n\n### License\n\n[MIT](https://opensource.org/licenses/MIT)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagileago%2Fvue3-oop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagileago%2Fvue3-oop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagileago%2Fvue3-oop/lists"}