All Projects → agileago → vue3-oop

agileago / vue3-oop

Licence: other
使用类和依赖注入写vue组件

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to vue3-oop

Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+2056.67%)
Mutual labels:  ioc, dependency-injection, decorators
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+2511.11%)
Mutual labels:  ioc, dependency-injection, decorators
Testdeck
Object oriented testing
Stars: ✭ 206 (+128.89%)
Mutual labels:  ioc, dependency-injection, decorators
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+47.78%)
Mutual labels:  ioc, dependency-injection, decorators
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (+374.44%)
Mutual labels:  ioc, dependency-injection, decorators
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+90%)
Mutual labels:  ioc, dependency-injection, decorators
DI-compiler
A Custom Transformer for Typescript that enables compile-time Dependency Injection
Stars: ✭ 62 (-31.11%)
Mutual labels:  ioc, dependency-injection
hypo-container
A dependency injection container.
Stars: ✭ 16 (-82.22%)
Mutual labels:  ioc, dependency-injection
sector
Simple Injector; Dependency Injection
Stars: ✭ 12 (-86.67%)
Mutual labels:  ioc, dependency-injection
waiter
Dependency injection, Inversion of control container for rust with compile time binding.
Stars: ✭ 71 (-21.11%)
Mutual labels:  ioc, dependency-injection
loopback-next
LoopBack makes it easy to build modern API applications that require complex integrations.
Stars: ✭ 4,412 (+4802.22%)
Mutual labels:  ioc, dependency-injection
ashley
Ashley is a dependency injection container for JavaScript.
Stars: ✭ 23 (-74.44%)
Mutual labels:  ioc, dependency-injection
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+2912.22%)
Mutual labels:  ioc, dependency-injection
Dry System
Organize your code into reusable components
Stars: ✭ 228 (+153.33%)
Mutual labels:  ioc, dependency-injection
stashbox
A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
Stars: ✭ 120 (+33.33%)
Mutual labels:  ioc, dependency-injection
Typedi
Simple yet powerful dependency injection tool for JavaScript and TypeScript.
Stars: ✭ 2,832 (+3046.67%)
Mutual labels:  ioc, dependency-injection
ioc-ts
Inversion of control container on TypeScript
Stars: ✭ 14 (-84.44%)
Mutual labels:  ioc, dependency-injection
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-75.56%)
Mutual labels:  ioc, dependency-injection
SwiftInjection
Dependency Injection framework for Swift
Stars: ✭ 21 (-76.67%)
Mutual labels:  ioc, dependency-injection
Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+192.22%)
Mutual labels:  ioc, dependency-injection

vue3 oop 文档

类组件+自动化的依赖注入(可选) = 极致的代码体验 DEMO

QQ交流群

前提条件

需要reflect-metadata 的支持

pnpm add @abraham/reflection injection-js 

项目入口需要引入 reflect-metadata

import '@abraham/reflection'

tsconfig.json 需要增加配置:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "useDefineForClassFields": false
  } 
}

安装

pnpm add vue3-oop 

vite配置

因为esbuild不支持装饰器的metadata属性,所以需要安装 @vue3-oop/plugin-vue-jsx 插件使用原始ts编译

定义组件

import { Autobind, ComponentProps, Computed, Hook, Link, Mut, VueComponent } from 'vue3-oop'
import { Directive, VNodeChild, watch } from 'vue'

const focusDirective: Directive = {
  mounted(el: HTMLInputElement) {
    el.focus()
  },
}

interface Foo_Props {
  size: 'small' | 'large'
  // 组件的slots
  slots: {
    item(name: string): VNodeChild
  }
}

class Foo extends VueComponent<Foo_Props> {
  // vue需要的运行时属性检查
  static defaultProps: ComponentProps<Foo_Props> = ['size']
  // 组件需要的局部指令
  static directives: Record<string, Directive> = {
    focus: focusDirective,
  }

  constructor() {
    super()
    // watch在构造函数中初始化
    watch(
      () => this.count,
      () => {
        console.log(this.count)
      },
    )
  }

  // 组件自身状态
  @Mut() count = 1

  // 计算属性
  @Computed()
  get doubleCount() {
    return this.count * 2
  }

  add() {
    this.count++
  }

  // 自动绑定this
  @Autobind()
  remove() {
    this.count--
  }

  // 生命周期
  @Hook('Mounted')
  mount() {
    console.log('mounted')
  }

  // 对元素或组件的引用
  @Link() element?: HTMLDivElement

  render() {
    return (
      <div ref="element">
        <span>{this.props.size}</span>
        <button onClick={() => this.add()}>+</button>
        <span>{this.count}</span>
        <button onClick={this.remove}>-</button>
        <div>{this.context.slots.item?.('aaa')}</div>
        <input type="text" v-focus/>
      </div>
    )
  }
}

定义服务

组件和服务的差距是缺少了render这一个表现UI的函数,其他都基本一样

class CountService extends VueService {
  @Mut() count = 1
  add() {
    this.count++
  }
  remove() {
    this.count--
  }
}

依赖注入

Angular文档

import { VueComponent, VueService } from 'vue3-oop'
import { Injectable } from 'injection-js'

// 组件DI
@Component({
  providers: [CountService]
})
class Bar extends VueComponent {
  constructor(private countService: CountService) {super()}

  render() {
    return <div>{this.countService.count}</div>
  }
}

@Injectable()
class BarService extends VueService {
  constructor(private countService: CountService) {super()}
}

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].