Registry / web-framework / vue-class-decorator

vue-class-decorator

JSON →
library7.6.3jsnpmunverified

Vue Class Decorator (current version 7.6.3) is a library that provides additional decorators for defining Vue 2 components using a class-based, TypeScript-first syntax. It extends the capabilities of `vue-class-component` and `vue-property-decorator` by introducing decorators for functional components (`@FunctionalVue`), filters (`@Filter`), event handling (`@On`, `@Once`), and lifecycle hooks (`@Mounted`). This approach allows developers to define components, methods, and lifecycle hooks as class properties and methods, leveraging TypeScript's strong typing. The library is specifically tailored for Vue 2 projects and does not support Vue 3, where the Composition API is the recommended approach for component definition. Its release cadence has slowed considerably, with no recent updates for Vue 3 compatibility, positioning it primarily for legacy Vue 2 maintenance.

npm install vue-class-decorator
INSTALL
IMPORT
SIG · VUE-CLASS-DECORATO
V
vue-class-decorator
web-frameworkjavascriptv7.6.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Component
import { Component } from 'vue-class-decorator'
import { Component } from 'vue-class-component'
While `@Component` originates from `vue-class-component`, it's often re-exported or used alongside other decorators from `vue-class-decorator` for convenience. Prefer importing from `vue-class-decorator` when using its other decorators.
FunctionalVue
import { FunctionalVue } from 'vue-class-decorator'
const { FunctionalVue } = require('vue-class-decorator')
This library is primarily designed for TypeScript and ESM environments. CommonJS `require` is generally not recommended.
Filter
import { Filter } from 'vue-class-decorator'
import Filter from 'vue-class-decorator'
All decorators and helper functions are named exports; there is no default export.
On
import { On } from 'vue-class-decorator'
Used for declarative event handling within the component instance.

Demonstrates the use of `@FunctionalVue`, `@Filter`, `@On`, and `@Mounted` decorators in a Vue 2 class-based component setup.

import Vue from 'vue'; import { Component, FunctionalVue, Filter, On, Mounted } from 'vue-class-decorator'; @Component export class DateComponent extends Vue { @Filter('formatDate') private formatDate(value: string): string { if (!value) return ''; const date = new Date(value); return date.toLocaleDateString(); } } @Component export default class MyFunctionalComponent extends FunctionalVue { message: string = 'Hello from functional!'; } @Component({ components: { DateComponent } }) export class EventHandlerComponent extends Vue { count: number = 0; @On('increment') handleIncrement(): void { this.count++; console.log('Incremented:', this.count); } @Mounted() onMounted(): void { console.log('EventHandlerComponent mounted!'); // Simulate emitting an event after 1 second setTimeout(() => { this.$emit('increment'); }, 1000); } } new Vue({ el: '#app', template: ` <div> <date-component :value="new Date().toISOString()"></date-component> <p>{{ '2023-10-26T10:00:00Z' | formatDate }}</p> <event-handler-component></event-handler-component> <p>Count in parent: {{ $children[2]?.count }}</p> </div> ` });
Debug
Known issues
breakingThis library is fundamentally designed for Vue 2 and is not compatible with Vue 3. Migrating to Vue 3 requires refactoring components to use the Composition API or an alternative community-maintained class component solution like `vue-facing-decorator`.
fix
For Vue 3, migrate to the Composition API or explore `vue-facing-decorator` or `@haixing_hu/vue3-class-component` as alternatives.
affects: >=7.x
gotchaUsing `@On` or `@Once` decorators with `reserve: false` will delete the decorated method from the component's `methods` object. This can lead to unexpected behavior if you later try to call the method directly (e.g., `this.myMethod()`) or through direct `$emit` listeners expecting it to exist.
fix
If the method needs to be directly callable or accessible via `this.methodName`, ensure `reserve` is `true` (which is the default) or omit it. Only set `reserve: false` if the method is exclusively for internal event binding and should not be part of the public `methods` API.
affects: >=7.x
gotchaFor TypeScript projects, the `tsconfig.json` must explicitly enable experimental decorators and emit decorator metadata. Without these settings, TypeScript will not correctly process the decorators, leading to compilation errors or runtime failures.
fix
Add or ensure the following compiler options in your `tsconfig.json`: `"experimentalDecorators": true, "emitDecoratorMetadata": true`.
affects: >=7.x
deprecatedThe class component style of Vue development, while supported by this library for Vue 2, is largely superseded by the Composition API in Vue 3. New Vue projects or migrations should prioritize the Composition API for better long-term maintainability and official support.
fix
For new projects or major refactoring, consider adopting the Vue 3 Composition API (`<script setup>`) to align with modern Vue development practices.
affects: >=7.x
Errors
Common errors & fixes
TypeError: Class extends value undefined is not a constructor or null
This error typically occurs when `vue-class-component` is not properly installed, configured, or its imports are incorrect. `vue-class-decorator` depends on `vue-class-component` for the base `Vue` class extension.
fix
Ensure `vue-class-component` is installed (`npm install vue-class-component`) and correctly imported. Also, verify that `Vue` is imported from `vue-class-component` when defining your base class, or that `vue-class-decorator`'s re-exports are correctly handled.
Support for the experimental syntax 'decorators' isn't currently enabled
Your TypeScript or Babel configuration does not have support for ECMAScript decorators enabled.
fix
In `tsconfig.json`, ensure `"experimentalDecorators": true` and `"emitDecoratorMetadata": true` are set under `compilerOptions`. If using Babel, ensure `@babel/plugin-proposal-decorators` and `@babel/plugin-proposal-class-properties` are configured.
Property 'myMethod' does not exist on type 'Vue'
This can happen if a decorated method's type inference is lost, or if you're trying to access a method that was removed by `@On`/`@Once` with `reserve: false`.
fix
Check the decorator settings, especially `reserve` for `@On`/`@Once`. Ensure all necessary types are correctly inferred. If it's a lifecycle hook, make sure it's decorated correctly (e.g., `@Mounted()`).
Upgrade
Version history
7.6.3latest on npm
Audit
Dependencies
vuerequiredPeer dependency for Vue 2 applications.
vue-class-componentrequiredFully depends on this library for class-style component base functionality.
vue-property-decoratorrequiredMany decorators are derived from or re-exported by this library, providing core decorator functionality.
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
vue-class-decorator — npm install vue-class-decorator · libregistry