Allow setup() and <script setup> to return a class instance in the composition API #623
Replies: 4 comments 16 replies
-
The issue with point 2 in my summary is that the instance returned from setup() seems to lose the functions in it's prototype, meaning that the class methods are gone. |
Beta Was this translation helpful? Give feedback.
-
What's the relationship with script setup? It looks like you want class-based components, an idea that has been previously discussed and that doesn't need script setup at all? Just a regular script block that exports a class? |
Beta Was this translation helpful? Give feedback.
-
This would be amazing! We have a legacy codebase that uses Knockout (class-based) ViewModels and those have to be bound to the template. I've tried a bunch of different patterns when all I really wanted was this: to be able to use the composition API to define a model. You can (sort of) do it with the Options API but then you have to accept all the awkwardness of the options API. |
Beta Was this translation helpful? Give feedback.
-
How is this related to Composition API? The class return example seems like an everything-goes options API, with the main benefit being that you can freely tag methods as lifecycle hooks with the help of decorators? Unless I'm missing something, it's more work than just using Composition API as is:
vs.
What's the benefit of having to name a watcher and rely on a new custom decorator? And what's the purpose of the return? Components return by default, don't they? The return seems to be the main thing of interest here? |
Beta Was this translation helpful? Give feedback.
-
As the title says I'd like to allow setup() and <script setup> to return a class instance and use that as the context. This would enable more features of Typescript such as decorators but keep the simplicity of the composition API.
The decorators used are purely used as an example and not meant to be included in the feature request.
Summary
<script setup>
which would work just like inexport default {setup() {}}
vue-facing-decorator also exists which does something similar, but does too much in my opinion and relying on it's support for professional products feels iffy.
I personally do not have enough knowledge of the Vue internals to make a PR request or package for this.
If anyone has any questions or needs clarification feel free to say so!
Beta Was this translation helpful? Give feedback.
All reactions