Skip to content

Vue 3.0 full state with classes and annotations. #459

Closed
@AliLozano

Description

@AliLozano

A month ago I was working in a concept to use Vue 3.0 / Composition API with Statefull classes, the result of this work is here:

https://codesandbox.io/s/test-vue-class-n0iuf?file=/src/components/HelloWorld.vue

  1. It works with annotations, I implement 4 annotations @watch @prop @Inject @context
  2. It have a vanilla JS Class without inheritance of Vue.
  3. It uses setup method with full state.
  4. It have getters/setters as computed properties
  5. It also exposes static properties, static getters and static methods.
  6. I think that it could be full safe typed (although my example is still missing many type declarations 🙄).
  7. It is small 🤭
import { Component, Inject, Prop, Context, Watch } from "./vue-class";

@Component()
export default class MyComponent {
  static VERSION = "1.2";

  @Inject()
  name: string = "";

  @Prop(String)
  msg: string;

  @Context()
  emit: any;

  newItem = "";

  items = ["Hello", "World"];

  setup() {
    // this is a reactive component :)
    console.log(this.msg);
  }

  @Watch("counter")
  watchCounter() {
    console.log("watch counter.. ");
    this.emit("hiFather");
  }

  get counter() {
    return this.items && this.items.length;
  }

  add() {
    this.items.push(this.newItem);
  }

  static uppercase(s: string) {
    return s && s.toUpperCase();
  }
  static get VERSION_NUMBER() {
    return parseFloat(this.VERSION) * 10;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions