Replies: 5 comments 13 replies
-
As explained in the original issue, that advantage will be pretty much non-existant with your proposal as class methods need to be bound so they can be passed around, i.e. for event listeners, class Store {
constructor() {
this.data = 'hello'
Object.assign(this, {
setData: this.setData
})
}
setData(v) { this.data = v }
}
const store = new Store()
const setData = store.setData
setData('goodbye') // TypeError: Cannot set property 'data' of undefined
console.log(store.data) With bind: class Store {
constructor() {
this.data = 'hello'
// creates a new function object in memory
this.setData = this.setData.bind(this)
}
setData(v) { this.data = v }
}
const store = new Store()
const setData = store.setData
setData('goodbye')
console.log(store.data) // => 'goodbye' |
Beta Was this translation helpful? Give feedback.
-
imho the idea is quite compelling, but needs to be fleshed out a little bit. performancethe pure techinal benefit is the performance gain you mention, but your example is a bit idealized for your argument. you would only see this kind of memory gain, if you have few components with LOTS of uses on a single render tree. but usually you have a LOT of different components a few times in the same listing. there are components that get used many times in a single render tree, but these are few and the ratio to the "unique" components is not that high and additionally, from my experience, the few components that indeed get used many times in a single render are usually the ones with few methods/computeds. same goes generally regarding time performance, but there you have the additional benefit of "recreated" instances that give a little more weight to the class case. so if performance should be an argument, it must be a less idealized and more realistic scenario used for the "prove". for what is this an alternative?i think using classes in the setup in the way presented here is an anti-pattern, because the main benefit of setup was meant to be composibility. yes ofc you can build small classes and compose a hierarchy, but this would diminish performance gains in real world use cases even more, since you just instantiate the same amount of classes as you would produce functions. also my guess is that writing up classes is more lines of codes then writing out some functions. instead i would argue this is more a replacement/alternative to the options API, if you go "all the way" for classes: export default defineComponent(class Counter {
public static props = {
value: Number,
};
public static setup(props): Counter {
const counter = new Counter();
counter.valueInternal = ref(props.value ?? 0);
return counter;
}
private valueInternal: Ref<number>;
@Bind()
public onAdd() {
this.valueInternal.value++;
}
@Bind()
public onSubtract() {
this.valueInternal.value--;
}
private someOtherMethod() {
// Statements
}
}); while i find this more appealing then a big object, it is basically still the same as vue-class-component, which works in vue 3. |
Beta Was this translation helpful? Give feedback.
-
You might be interested in looking at this where I explored how much Answer is actually quite a lot. This looks even nicer if instead of Today I see only one big downside to this programming model: to be perfect it really needs decorators, which after 6+ years are still... let's say far from done. That's quite a bummer to me. The aversion of some prominent JS figures towards classes is IMHO misplaced. The are a pretty good tool to build UI, as demonstrated by the many existent UI frameworks. It'd be nice if they got some support in Vue 3 Composition. If you squint a bit, |
Beta Was this translation helpful? Give feedback.
-
Use class style to write setup and support vue2 and vue3 |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I have created a vue3 framework with ioc container, named as You can find it here: https://github.com/cabloy/zova Do you agree with such a concept: Class should not be used in the Therefore, it is completely different from Zova has a very important feature: With the support of ioc container, defining reactive states no longer needs Demonstration: no
|
Beta Was this translation helpful? Give feedback.
-
I just discovered that we could use ES6 class for creating components instead of the Composition API setup function. The best thing is it works out of the box in Vue 3 without any dependency on
vue-class-component
or other libraries! Even the experimental template interpolation service in Vetur is working flawlessly with it!!!Surprisingly I couldn't find this way of creating components in any of Vue 3 documentation. Not sure why, or am I missing something!?
With official support, we could make the experience even better.
Why use ES6 class:
In the current way of creating a component using setup function & composition API, users are encouraged to create closure functions. When the number of instances of a component is minimal this doesn't make much difference. But when there are so many instances of the component, multiples of those many functions objects would be created, which doesn't look ideal.
For instance, 1000 instances of the following component would create 1000
getUserRepositories
function objects. Which is overkill.What does the proposed API look like?
This is how we could create a Counter component using ES6 class & composition API. And it works with the current release of Vue 3!
I have committed a dummy project with working example @ https://github.com/sreenaths/vue3-composition-class
What could we do through official support:
There could be more, but the following are some of the items that came to my mind.
export default Counter
instead of this working snippetexport default defineComponent(props => new Counter(props));
.vuejs/core#3373 - This is the issue that I originally created for this discussion
Beta Was this translation helpful? Give feedback.
All reactions