Vue Class Decorator

7.6.3 · maintenance · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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>
  `
});

view raw JSON →