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

vue-clazz-decorator

v0.1.0

Published

Write Vue 3 components with Class + Decorators, featuring ViewModel/View separation, metadata system, and reactive model layer.

Readme

vue-clazz-decorator 让你在 Vue 3 中使用 Class + 装饰器 的风格来写组件。

npm version license CI

特色

  • 通过类的方式来写 Vue 组件
  • 方便地复用模板和逻辑
  • 保证自定义类型的纯净
  • 支持多种装饰器编译方式
  • 兼容 reflect-metadata API
  • 完整的数据模型层:序列化/反序列化/类型转换

如果你喜欢 vue-class-component 或 vue-property-decorator, 那么请试试这个项目吧!

创建组件

这个库的核心思想是 组件 = 模板 + 业务逻辑。这是 createComponent 设计的体现,避开 Vue 3 不能用 class 组件的同时,顺便能复用模板和逻辑。

@ViewModel
export class CounterViewModel {
    @State
    public count = 0;

    @Computed
    public get doubleCount() {
        return this.count * 2;
    }

    public increment() {
        this.count++;
    }
}
export function CounterView(props: CounterViewModel) {
    return <div>
        <p>Count: {props.count}</p>
        <button onClick={props.increment}>+1</button>
    </div>;
}

export const Counter = createComponent(CounterView, CounterViewModel);

组件生命周期

使用注解声明生命周期方法,不强制固定方法名,一个钩子可以绑定多个方法,保证类的纯净。

@ViewModel
export class FooViewModel {
    @OnDidCreate
    protected init() {
        console.log("ViewModel created");
    }

    @OnDidMount
    protected xxxx() {
        console.log("componentDidMount");
    }
    @OnDidMount
    protected yyyy() {
        console.log("componentDidMount again");
    }
}
export function FooView(props: FooViewModel) {
    return <div></div>;
}

export const Foo = createComponent(FooView, FooViewModel);

组件逻辑复用

业务逻辑自动就包在命名空间里,非常易于管理。

@ViewModel
export class FooViewModel {
}
@ViewModel
export class BarViewModel {
    public foo1 = use(FooViewModel);
    public foo2 = use(FooViewModel);
}
export function BarView(props: BarViewModel) {
    return <div></div>;
}

export const Bar = createComponent(BarView, BarViewModel);

模型层声明

@Model 标记数据模型类,结合元数据描述字段含义,再通过 reactive() 将普通数据转为响应式实例,同时保留类型信息。

@Model
export class UserBo {
    @metadata('label', "用户ID")
    public id: string;

    @metadata('label', "用户名")
    public name: string;
}
var user = reactive({
    id: "admin",
    name: "超级管理员",
}, UserBo);

如何写一个注解

库内置的metadata函数可以便捷地帮助我们创建一个注解。

// 定义一个注解
function Label(text: string) {
    return metadata('label', text);
}

// 使用
@Model
export class UserBo {
    @Label("用户ID")
    public id: string;

    @Label("用户名")
    public name: string;
}

// 获取元数据
getMetadataValues(UserBo);
// { id: { label: "用户ID"}, name: { label: "用户名" } }

支持的装饰器编译方式

项目支持多种方式,并可以混合使用。

  • experimentalDecorators: false + useDefineForClassFields: true 以下简称 proposal
  • experimentalDecorators: true + useDefineForClassFields: false 以下简称 experimental
  • experimentalDecorators: true + useDefineForClassFields: false + emitDecoratorMetadata: true 以下简称 metadata

| 编译方式 | proposal | experimental | metadata | | --- | --- | --- | --- | | typescript | ✅ 支持 | ✅ 支持 | ✅ 支持 | | babel | ✅ 支持 | ✅ 支持 | ✅ 支持 |


文档导航

功能清单

| 分类 | API | | --- | --- | | 组件创建 | createComponent use useChildren | | 类装饰器 | @ViewModel @Model | | 数据装饰器 | @State @Reactive @Computed @Ref @Watch | | 组件通信装饰器 | @Prop @Emit @ModelValue @Provide @Inject | | 生命周期装饰器 | @OnDidCreate @OnWillMount @OnDidMount @OnWillUpdate @OnDidUpdate @OnWillUnmount @OnDidUnmount @OnDidCatch | | JSON 序列化装饰器 | @JsonProperty @JsonExpose @JsonIgnore @JsonSerialize @JsonDeserialize @JsonFormat | | 数据类型装饰器 | @Type ArrayType ReactiveArrayType @From | | 元数据操作函数 | metadata defineMetadata defineClassMetadata defineFieldMetadata getClassMetadataValues getFieldMetadataValues | | 元数据查询函数 | getMetadata getOwnMetadata getMetadataKeys getOwnMetadataKeys hasMetadata hasOwnMetadata deleteMetadata | | 兼容 reflect-metadata 函数 | metadata hasOwnMetadata getOwnMetadataKeys getOwnMetadata getMetadata deleteMetadata defineMetadata | | 模型实例化函数 | reactive normalize hydrate | | 其他导出 | nextTick state computed |


许可证

MIT