What are the advantages of 'setup' hook in Vue3?

Viewed 138

Personally, I think the syntax of vue2 where method, props, and data are distinguished is better, but I'm curious as to why setup was created.

2 Answers

This is pretty well explained into the official guide of the migration to Vue3: https://v3.vuejs.org/guide/composition-api-introduction.html#why-composition-api

What you do call "Vue2 syntax" is actually Options API.
While the Composition API ("Vue3 syntax") is mainly for:

  • not being locked to have your functions locked to specific parts of your app
  • reusability of functions, mainly Composables
  • having a more "vanilla" JS syntax, rather than being more specific to Vue
  • being able to use the latest advanced methods coming with Vue3

One thing to remember is that you can totally use Options API while using Vue3, it's retro-compatible and totally fine. You can even mix both in the same file too!

Related