New Ways to Vue

Rate this content
Bookmark

Brief intro to the new features of Vue 3 and Vite, then focus on how those features / mindset affect the way we think of tooling and DX. Which leads to my Vitesse project and several unplugins that enhance the Vue developer experience even further (will do some short demos in between). Finally, introduce the ways to use those new features in the existing Vue 2 apps today and make the migrations easier.

This talk has been presented at Vue.js London Live 2021, check out the latest edition of this JavaScript Conference.

FAQ

Vue 3's Composition API is a new set of API that simplifies the organization of logic in applications. Unlike the Options API, it uses a single setup function where all the logic, including lifecycle functions as hooks, is contained. This approach enhances TypeScript support and improves code reusability and composability.

The script setup syntax introduced in Vue 3.1 and stabilized in Vue 3.2 allows developers to declare variables, functions, and components at the top level of a script, making them directly available inside the component. This reduces scaffolding code and simplifies component structure, enhancing clarity and development speed.

Vite is designed specifically for web development, leveraging the browser's native ESM support. It starts the server immediately and transpiles modules on demand, providing faster hot module replacement and better performance during development compared to traditional bundlers that require complex configurations and are slower as the project grows.

Components auto-importing in Vite allows developers to automatically import components in Vue applications. Using compile-time component resolving, this feature skips manual registration and runtime resolving, leading to better code splitting and improved performance.

The Vite plugin inspect provides a UI that helps developers inspect the intermediate state of module transformations and the relationships between modules. This is particularly useful for debugging plugins and understanding how Vite works internally, aiding both plugin authors and beginners.

Unplugin is a universal plugin interface that allows developers to write plugins once and run them across multiple major tools and frameworks like Webpack, Vite, and Rollup. By wrapping traditional plugins with the createUnplugin function, Unplugin facilitates cross-platform compatibility and extends the utility of existing plugins.

Yes, Vue 2 users can access Vue 3 features such as the Composition API through polyfills and specific packages like vue-composition-api. Additionally, plugins like Unplugin vue 2 square setup and Vite plugin Vue 2 help integrate these features into Vue 2 projects, enhancing functionality and developer experience.

Anthony Fu
Anthony Fu
16 min
21 Oct, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
The Talk discussed new ways of using Vue, including the introduction of the composition API and the script setup syntax. The Vite tooling was highlighted for its performance improvements and developer experience enhancements. Components auto-importing through Vite plugin components was introduced as a way to improve code splitting and eliminate manual registration. The use of Vite plugins, Unplugin, and Vue 2 support were also discussed. The Talk mentioned that Nuxt 3 will include many of these features.
Available in Español: Nuevas formas de Vue

1. Introduction to New Ways of Vue

Short description:

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. In Vue three, we introduced a new set of API called a composition API. In composition API, we have only single function called a setup. Lifecycle functions are now providing as hooks, so we can have a better TypeScript support and better composability.

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.

2. Script Setup Syntax and Vite Tooling

Short description:

In the new script setup syntax, variables, functions, components inside of the script setup are directly available inside of the components. The V-bind syntax inside the style tag allows for better organization of the template and the style. Vue 3 and Vite bring better performance, developer experience, and new ways to Vue. With the new SQL setup syntax, we no longer need to register components. Components auto-importing through Vite plugin components provides better code splitting and eliminates the need for manual registration.

So let's talk about the script setup syntax. As you can see, even with the new composition API, we are having many scaffolding codes in our components. For example, in this case, the highlight lines are the things we actually care about, but we will need to write the rest in order to have Vue understand this. In the new script setup syntax, we can have them all declare at top level. And variables, functions, components inside of the script setup are directly available inside of the components. This is initially introduced in Vue 3.1 as an experimental feature and now it's stable in Vue 3.2.

The other new feature is the V-bind syntax inside the style tag. In the past, if we want to have dynamic styling components, we will need to first declare some reactivity data inside of the data, and then we kind of bind the data inside of the template. But now with V-bind, we can have the binding inside of our style tag so we can have better organization of the template and the style.

So finally, let's talk about the new default tooling, Vite. So what is Vite? I bet many of you have already heard about that, but in case you don't, let's have a quick introduction. So first, we used to have bundlers like Webpack and Roa. The problem of them are they are usually designed for production build first and need to bundle the entire projects in order to start the dev server. It also involves complex configurations and the hard module replacements get slower as the project grows. So now we have dev servers, for example, Snowpack and Vite. So in dev servers, we are designed specifically for web development. We leverage the browser's native ESM support, and we don't need to bundle the code anymore. The server started immediately, and we only transpiled the modules on demand. In this way, we also have instance hard module replacements and much more possibilities. So what do Vue 3 and Vite bring to us? Not only the better performance and the better developer experience, but also the new ways to Vue. So let's have a look at how we use Vue components. So first, we need to import and name it and then register the components so that we can use it in the template. So the problem here is that it makes our code quite verbose, and the name of each component are repeated at least four times. Not only this slows down our development, but also renaming components becomes quite frustrating. So with the new SQL setup syntax, we no longer need to register the components anymore. But the thing is that the name is still repeated three times. So what if we can make it better? Introduce components auto-importing through Vite plugin components. Since we already know the name of each component, we can actually directly mapping them to use it in the template. So how we did that? We use compile-time components resolving for components on the source slash components directory. The usage looks like the global registrations, but the difference from them is that with the auto-importing, we can provide a better code splitting, and we can no longer need to do the manual registration.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Vite: Rethinking Frontend Tooling
JSNation Live 2021JSNation Live 2021
31 min
Vite: Rethinking Frontend Tooling
Top Content
Vite is a next-generation build tool that leverages native ES modules for improved performance. It eliminates the need for bundling and improves hot module replacement. Vite provides an opinionated default configuration while still allowing advanced customization through plugins. It is framework agnostic and can be used for React and other applications. Vite is being adopted by Next.js and Create React App, and integration with Nuxt 3 offers significant speed improvements.
Everything Beyond State Management in Stores with Pinia
Vue.js London Live 2021Vue.js London Live 2021
34 min
Everything Beyond State Management in Stores with Pinia
Top Content
State management is not limited to complex applications and transitioning to a store offers significant benefits. Pinia is a centralized state management solution compatible with Vue 2 and Vue 3, providing advanced devtools support and extensibility with plugins. The core API of Pinia is similar to Vuex, but with a less verbose version of stores and powerful plugins. Pinia allows for easy state inspection, error handling, and testing. It is recommended to create one file per store for better organization and Pinia offers a more efficient performance compared to V-rex.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
One Year Into Vue 3
Vue.js London Live 2021Vue.js London Live 2021
20 min
One Year Into Vue 3
Top Content
Vue 3 has seen significant adoption and improvements in performance, bundle size, architecture, and TypeScript integration. The ecosystem around Vue 3 is catching up, with new tools and frameworks being developed. The Vue.js.org documentation is undergoing a complete overhaul. PNIA is emerging as the go-to state management solution for Vue 3. The options API and composition API are both viable options in Vue 3, with the choice depending on factors such as complexity and familiarity with TypeScript. Vue 3 continues to support CDN installation and is recommended for new projects.
Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
Vue: Feature Updates
Vue.js London 2023Vue.js London 2023
44 min
Vue: Feature Updates
Top Content
Watch video: Vue: Feature Updates
The Talk discusses the recent feature updates in Vue 3.3, focusing on script setup and TypeScript support. It covers improvements in defining props using imported types and complex types support. The introduction of generic components and reworked signatures for defined components provides more flexibility and better type support. Other features include automatic inference of runtime props, improved define emits and defined slots, and experimental features like reactive props destructure and define model. The Talk also mentions future plans for Vue, including stabilizing suspense and enhancing computer invalidations.

Workshops on related topic

Vue3: Modern Frontend App Development
Vue.js London Live 2021Vue.js London Live 2021
169 min
Vue3: Modern Frontend App Development
Top Content
Featured WorkshopFree
Mikhail Kuznetsov
Mikhail Kuznetsov
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.

Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.

Table of contents:
- Introduction to Vue3
- Composition API
- Core libraries
- Vue3 ecosystem

Prerequisites:
IDE of choice (Inellij or VSC) installed
Nodejs + NPM
Monitoring 101 for React Developers
React Summit US 2023React Summit US 2023
107 min
Monitoring 101 for React Developers
Top Content
WorkshopFree
Lazar Nikolov
Sarah Guthals
2 authors
If finding errors in your frontend project is like searching for a needle in a code haystack, then Sentry error monitoring can be your metal detector. Learn the basics of error monitoring with Sentry. Whether you are running a React, Angular, Vue, or just “vanilla” JavaScript, see how Sentry can help you find the who, what, when and where behind errors in your frontend project. 
Workshop level: Intermediate
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Vue.js London Live 2021Vue.js London Live 2021
117 min
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Top Content
Workshop
Daniel Roe
Daniel Roe
We'll build a Nuxt project together from scratch using Nitro, the new Nuxt rendering engine, and Nuxt Bridge. We'll explore some of the ways that you can use and deploy Nitro, whilst building a application together with some of the real-world constraints you'd face when deploying an app for your enterprise. Along the way, fire your questions at me and I'll do my best to answer them.
Going on an adventure with Nuxt 3, Motion UI and Azure
JSNation 2022JSNation 2022
141 min
Going on an adventure with Nuxt 3, Motion UI and Azure
WorkshopFree
Melanie de Leeuw
Melanie de Leeuw
We love easily created and deployed web applications! So, let’s see what a very current tech stack like Nuxt 3, Motion UI and Azure Static Web Apps can do for us. It could very well be a golden trio in modern day web development. Or it could be a fire pit of bugs and errors. Either way it will be a learning adventure for us all. Nuxt 3 has been released just a few months ago, and we cannot wait any longer to explore its new features like its acceptance of Vue 3 and the Nitro Engine. We add a bit of pizzazz to our application with the Sass library Motion UI, because static design is out, and animations are in again.Our driving power of the stack will be Azure. Azure static web apps are new, close to production and a nifty and quick way for developers to deploy their websites. So of course, we must try this out.With some sprinkled Azure Functions on top, we will explore what web development in 2022 can do.
TresJS create 3D experiences declaratively with Vue Components
Vue.js London 2023Vue.js London 2023
137 min
TresJS create 3D experiences declaratively with Vue Components
Workshop
Alvaro Saburido
Alvaro Saburido
- Intro 3D - Intro WebGL- ThreeJS- Why TresJS- Installation or Stackblitz setup - Core Basics- Setting up the Canvas- Scene- Camera- Adding an object- Geometries- Arguments- Props- Slots- The Loop- UseRenderLoop composable- Before and After rendering callbacks- Basic Animations- Materials- Basic Material- Normal Material- Toon Material- Lambert Material- Standard and Physical Material- Metalness, roughness - Lights- AmbientLight- DirectionalLight- PointLights- Shadows- Textures- Loading textures with useTextures- Tips and tricks- Misc- Orbit Controls- Loading models with Cientos- Debugging your scene- Performance
Building Vue forms with VeeValidate
Vue.js London Live 2021Vue.js London Live 2021
176 min
Building Vue forms with VeeValidate
Workshop
Abdelrahman Awad
Abdelrahman Awad
In this workshop, you will learn how to use vee-validate to handle form validation, manage form values and handle submissions effectively. We will start from the basics with a simple login form all the way to using the composition API and building repeatable and multistep forms.

Table of contents:
- Introduction to vee-validate
- Building a basic form with vee-validate components
- Handling validation and form submissions
- Building validatable input components with the composition API
- Field Arrays and repeatable inputs
- Building a multistep form
Prerequisites:
VSCode setup and an empty Vite + Vue project.