vuejs / vue-class-component

ES / TypeScript decorator for class-style Vue components.
MIT License
5.81k stars 431 forks source link

I have implemented the class API based on setup, which is friendly to TS types and supports vue2 and vue3 #624

Open lzxb opened 1 year ago

lzxb commented 1 year ago

vue-class-component has been widely used in our projects. vue-class-setup hopes to get official support and further promote the best practices of Vue class. @ktsn

See: https://github.com/fmfe/vue-class-setup

image

image
jaeming commented 1 year ago

This looks very promising. I'll be sure to check it out. Thanks!

lzxb commented 1 year ago

This looks very promising. I'll be sure to check it out. Thanks!

Our internal component library is developed based on it

wlhyl commented 1 year ago

vue-class-setup正是所需要的。nice!

rootux commented 1 year ago

Love it - will give it a try

thalysmg commented 1 year ago

One doubt: "Use class style to write setup and support vue2 and vue3". Does it mean that it's purpose is only to write setup, or can we write components just the same way we do in vue-class-component? I didn't get it right.

lzxb commented 1 year ago

One doubt: "Use class style to write setup and support vue2 and vue3". Does it mean that it's purpose is only to write setup, or can we write components just the same way we do in vue-class-component? I didn't get it right.

It is not a class component, but it can use classes to write setup, which can be better supported by TS.

lzxb commented 1 year ago

I have used it on a large scale in our company, involving as many as 100+ projects, and it can run well.

lzxb commented 1 year ago

<script lang="ts">
import { defineComponent } from 'vue';
import { Setup, Context } from 'vue-class-setup';

// Setup and Context must work together
@Setup
class App extends Context {
    private _value = 0;
    public get text() {
        return String(this._value);
    }
    public set text(text: string) {
        this._value = Number(text);
    }
    public onClick() {
        this._value++;
    }
}
export default defineComponent({
    // Inject setup
    ...App.inject(),
});
</script>
<template>
    <div>
        <p>{{ text }}</p>
        <button @click="onClick()"></button>
    </div>
</template>

This is the simplest example, and it is somewhat similar to a class component.

lzxb commented 1 year ago

It cannot export a class like a class component. It needs to be put into defineComponent for use, so that type inference can be obtained

thalysmg commented 1 year ago

It cannot export a class like a class component. It needs to be put into defineComponent for use, so that type inference can be obtained

Perfect! Thank you for clarifying my doubt.

Is there any forecast to get official support? I'm afraid of starting to use it, and it just gets abandoned like they did with vue-class-component.

I'm starting to migrate my class components to normal ts components because of that...

lzxb commented 1 year ago

It cannot export a class like a class component. It needs to be put into defineComponent for use, so that type inference can be obtained

Perfect! Thank you for clarifying my doubt.

Is there any forecast to get official support? I'm afraid of starting to use it, and it just gets abandoned like they did with vue-class-component.

I'm starting to migrate my class components to normal ts components because of that...

There is no official reply at present, but I will maintain it.