-
Notifications
You must be signed in to change notification settings - Fork 542
This <3 Vue #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It also fixes those nasty circular typing issues, granted you:
Edit: Scrap that, the getter is returning a different computed everytime. It's not what we want. It does not really fix the TS issues I guess. |
This is cool. 👍 Is it a bug that the handler defined in the setup function don't get bound to the context? If we define the handler in methods option, or use invocation in the template instead of method name ( In fact, we could use getter function directly without So this will work: const App = defineComponent(() => ({
items: ["Hello", "World"],
newItem: "",
add() {
this.items.push(this.newItem);
this.newItem = "";
},
remove(item) {
this.items.splice(this.items.indexOf(item), 1);
},
get count() {
return this.items.length;
}
})); We just blend the |
Not a bug, by design. AFAIK the message for Vue 3 is: never use Regarding event handlers, if you use this in your template: <button @click="do()" /> Then it gets compiled to On the other hand, if you use: <button @click="do" /> Vue will store that function and later invoke it with an undefined
Of course! It doesn't behave exactly the same, though. Because of all this, as long as:
Then you can just return a plain object and everything works, which is nice for simple components. The demo I made above use a helper
And that covers quite a bit of ground for simple components. Just to be clear: this is not an attempt at replacing the Vue 3 model, which gives you lots of power and flexibility and is required for other stuff such as watchers, hooks, etc. It's an attempt at removing ceremony and simplifying usage in simple components that are just state + methods. Sounds silly but quite a few component fall into that category in practice and I find myself doing this pattern a lot: setup() {
const state = reactive({
...
});
return state;
|
Totally understandable. But as for event handlers, the distinction between merely the presence or absence of parentheses, to me, seems unreasonable.
Am I missing something, doesn't
Wait, so the getters in
Definitely! I was just tried to demonstrate that how clean it could be for simple components like this. |
You can actually do exactly the same thing with export default {
data() {
return {
value: 1,
get counter() { return 'counter: ' + this.counter },
increment: () => { this.value++ }
}
}
} |
This issue was part of some experiments I did during the Vue 3 betas to check how the new composition API could, with small tweaks, lend itself to define components with classes. 2 years later Vue 3 shipped and the UX improvements went in different directions like <script setup>, so I don't think there's much to say or act upon in this issue -> I'm closing it. |
I made something that I think is worth discussing here.
I made
this
work with Vue 3.For some typical components, it leads to very simple code, check this:
Magic?
You can test it and see how the trick is done here:
https://codesandbox.io/s/nice-engelbart-259gq
The text was updated successfully, but these errors were encountered: