Hello everyone, glad to be here at Vue London. Today my topic is new ways to Vue, how the new tools and techniques affect the way we Vue and build applications. My name is Anthony Fu, and I'm a Vue and Vite co-working member. I'm also a creator of SlideDev, Vueuse, VDesk, and other open source projects. I'm a fanatic open sourcer, currently working at Nuxt Labs. My GitHub handle is antfu, you can also find me on Twitter. Before I start, I want to thank all my sponsors for supporting my work. If you find my work useful, you can also sponsor me at GitHub, it would mean a lot to me.
So let's talk about today's topic, new ways to Vue. So let's talk about the Vue two-way first. Under that, we have Vue single file components. In this component, we have template tag and a script tab. In script tab, we'll need to import the things we want. For example, import Vue from Vue, and then export the default components objects using Vue.extend, and then we'll need to register our components, make things, and then declare data and method. The problem here is that we are having too much Scala folding code for each components, and also the mixings are kind of limited for the extensibilities to reuse our code and also having some problem of TypeScript support.
To solve this, in Vue three, we introduced a new set of API called a composition API. Let's do a quick comparison here. On the left, we have the options API, and on the right, we have the new composition API. As you can see, in options API, we used to have multiple properties for the object. For example, they have method, create, and so on. But in composition API, we have only single function called a setup. Lifecycle functions are now providing as hooks, so you can use it inside of our setup functions. So we have everything inside of the single context. This way, we can have a better TypeScript support. But other than that, most importantly, we can have a better composability. For example, we have a component with the setup functions. If we want to reuse this logic, we can simply copy over it and having it inside of new files and wrapping with the functions. In this case, we call it useDark. So then we can refactor our components to import the useDark functions and reuse it. The components will behave exactly the same as before, but we can now reuse our functions inside of other components and have better organizations of our logic.
Comments