vue-facing-decorator

repository·master·Indexed 19 days ago

https://github.com/facing-dev/vue-facing-decorator

A library for Vue 3 that enables writing components using the Class API and decorators, serving as a modern successor to vue-class-component and vue-property-decorator. It supports TypeScript, Stage 2 and Stage 3 decorators, and ES class inheritance. Key features include the @Component, @Prop, and @Emit decorators, as well as the toNative function for casting class components to the Vue Options API.

Tokens
14.3K
Snippets
67
Records
84
Agent score
61%

What's inside vue-facing-decorator

  1. Overview of vue-facing-decorator

    master

    vue-facing-decorator is a library designed for Vue 3 that allows developers to write Vue components using ES classes. It is intended as a modern successor to the deprecated vue-class-component and vue-property-decorator packages.

    Key features include:

    • TypeScript & Decorator Support: Works with both TypeScript and standard decorators.
    • Decorator Compatibility: Supports both Stage 2 and Stage 3 decorators.
    • Project Flexibility: Compatible with both TypeScript and JavaScript projects.
    • Safety & Performance: Transforms ES classes into Vue Options API components according to specifications, with transformations occurring during project loading for high performance.
    • Inheritance Support: Supports ES class inheritance, Vue extends, and Vue mixins.
  2. Understand complex inheritance relationships in vue-facing-decorator

    master

    When using vue-facing-decorator, inheritance follows a specific hierarchy involving ECMAScript classes and Vue mixins. In complex scenarios where multiple levels of inheritance are combined with Vue's native mixins mechanism, Vue's implementation rules apply.

    Specifically, if a component uses Vue mixins (via VueNativeComponent), it will overwrite properties or methods from a component that uses Vue extends (via SuperComp).

    Inheritance Hierarchy Example:

    1. Comp (ECMAScript extends CompSuper) uses Vue mixins [VueNativeComponent].
    2. SuperComp (ECMAScript extends SuperCompSuper) uses Vue extends.
    3. VueNativeComponent is the base Vue component.

    In this structure, the mixins applied to Comp take precedence over the extends applied to SuperComp due to how Vue resolves component options.

    // The inheritance relationship follows this logic:
    // (Comp ECMAScript extends CompSuper)
    //     vue mixins [VueNativeComponent]
    //         vue extends (SuperComp ECMAScript extends SuperCompSuper)
    // 
    // Result: VueNativeComponent (mixins) overwrites SuperComp (extends)
  3. Enable Stage 3 decorators in vue-facing-decorator

    master

    To use Stage 3 decorators instead of the standard Stage 2 decorators, you must update your environment to support the new specification.

    Note: Some third-party libraries (such as esbuild when compiling TypeScript code inside .vue files) may not yet support Stage 3 decorators. Because of this, using Stage 2 decorators is generally recommended for stability.

    // tsconfig.json
    {
      "compilerOptions": {
        "experimentalDecorators": false
      }
    }
  4. Use the @Provide decorator to provide dependencies in a class component

    master

    In vue-facing-decorator, you can use the @Provide decorator to make a dependency available to all descendant components in the component tree. This follows the standard Vue provide/inject pattern but allows you to declare dependencies directly on class properties within a class component.

    @Provide()
    myDependency = 'some value';
  5. Use standard Vue lifecycle hooks in Class Components

    master

    In a vue-facing-decorator class component, you can use almost all standard Vue lifecycle hooks by defining them directly as class methods. These hooks are recognized by the decorator and are not transformed into standard component methods.

    import { Component, Vue } from 'vue-facing-decorator';
    
    @Component
    export default class MyComponent extends Vue {
      created() {
        console.log('Component created!');
      }
    
      mounted() {
        console.log('Component mounted!');
      }
    }
  6. Enable TypeScript Decorators Stage 3

    master

    In vue-facing-decorator v3.0.0+, you can use the TypeScript Decorators API Stage 3. To enable this, you must upgrade to TypeScript 5.x and configure your tsconfig.json by setting compilerOptions.experimentalDecorators to false. If you need to use Stage 2 decorators, keep the value as true.

    {
      "compilerOptions": {
        "experimentalDecorators": false
      }
    }