Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Vue.js 3 Design Patterns and Best Practices
Vue.js 3 Design Patterns and Best Practices

Vue.js 3 Design Patterns and Best Practices: Develop scalable and robust applications with Vite, Pinia, and Vue Router

By Pablo David Garaguso
₹800 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (2 Ratings)
Book May 2023 296 pages 1st Edition
eBook
₹2,382.99 ₹800.00
Print
₹2,978.99
Subscription
₹800 Monthly
eBook
₹2,382.99 ₹800.00
Print
₹2,978.99
Subscription
₹800 Monthly

What do you get with a Packt Subscription?

Free for first 7 days. ₹800 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : May 30, 2023
Length 296 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781803238074
Category :
Table of content icon View table of contents Preview book icon Preview Book

Vue.js 3 Design Patterns and Best Practices

The Vue 3 Framework

The world wide web of today has changed by many magnitudes since the early days when the internet was just a collection of linked pages for academic and scientific purposes. As the technology evolved and machines became more powerful, more and more features were added to the earlier protocols, and new techniques and technologies competed until finally, standards were adopted. Extra functionality came in the form of plugins for the browser and embedded content. Java applets, Flash, Macromedia, Quicktime, and other plugins were common. It was with the arrival of HTML5 that most, if not all, of these were gradually replaced by standards.

Today, a clear distinction exists between structure, style, and behavior. Hyper Text Markup Language (HTML) defines the structural elements that make up a web page. Cascading Style Sheets (CSS) provides rules that modify the appearance of HTML elements, including even animations and transformations. And finally, JavaScript is the...

The progressive framework

Before we describe what Vue is, we need to make the distinction between the terms library and framework. These are often used interchangeably, but there is a difference, and a good developer should be aware of this when choosing one or the other to build a web application.

Let’s have a look at the definitions of these terms:

  • A library is a collection of reusable code, in the form of functions, classes, and so on, that have been developed by someone else and can be easily imported into your program. It does not prescribe how and where to use it, but normally, they provide documentation on how to use them. It is up to the programmer to decide when and how to implement them. This concept exists in most development languages, to the point that some of them are completely based on the notion of importing libraries to provide functionality.
  • A framework also has bundles of classes and functions for your use but prescribes specifications that...

Using Vue in your web application

There are several options to use Vue in your web application, and it largely depends on what your objective is:

  • To include a small self-contained application or piece of code on a page, you can directly import Vue and code inside a script tag
  • To build a larger application, you will need a build tool that takes your code and bundles it for distribution

Notice that I use the word bundle and not compile, as JavaScript applications are interpreted and executed at runtime on the browser. This will become apparent later on when we introduce the concept of single-file components.

Let’s briefly see an example of the first case in a very simple HTML page:

<html>
<head>
    <script src="https://unpkg.com/vue@3"></script>
</head>
<body>
    <div id="app">
    {{message}}
    </div>...

Understanding single-file components

As you may have guessed, the App.vue file mentioned before is a single-file component (SFC), one of the great contributions of Vue. In this type of file, we can describe the HTML, CSS, and JavaScript that defines the component. The web page is then composed in a hierarchy of components, starting from an entry point (traditionally named App.vue) down to the last customized button, if you will. We will discuss components in depth in Chapter 4, User Interface Composition with Components, but for now, keep in mind that this is the way to go, as prescribed by the framework. If you have experience in an object-oriented language, this may look familiar (and you wouldn’t be wrong).

An SFC is a plain text file with the .vue extension that contains the following sections:

<script setup>
    // Here we write our JavaScript
</scrip>
<template>
    <h1>Hello World! This is pure HTML...

Different strokes – options, composition, and script setup API

The classical way to describe a component in Vue 2 has been branded as the Options API. To maintain backward compatibility, the same syntax is also supported in Vue 3. However, there is also a new syntax named the Composition API, which is what we will use in this book.

The Options API is inherited from Vue 2 and prescribes that a component is defined by an object with defined fields, none of which is actually mandatory. Moreover, some of them have defined parameters and expected outputs. For example, these are the most common fields to use (also, a non-exclusive list):

  • data should be a function that returns an object whose fields would become reactive variables.
  • methods is an object that contains our functions. These functions have access to the reactive variables from data by using the this.variableName format.
  • components is an object where each field provides a name for the template, and the...

Exploring built-in directives in Vue 3

Vue also provides special HTML attributes called directives. A directive is declared in the opening tag of an HTML element and will affect or provide dynamic behavior or functionality to that element. We can also create our own directives in Vue. Those provided by the framework have a special notation starting with v-. As for the purpose of this book, let’s explain the most commonly used Vue directives:

v-bind: (shorthand ":")

The v-bind: directive binds the value of an HTML attribute to the value of a JavaScript variable. If the variable is reactive, each time it updates its value, it will be reflected in the html. If the variable is not reactive, it will be used only once during the initial rendering of the HTML. Most often, we use only the : shorthand prefix (semi-colon). For example, the my_profile_picture reactive variable contains a web address to a picture:

<img :src="my_profile_picture">

The...

Built-in components

The framework also provides us with several built-in components that we can use without explicitly importing them into each SFC. I have provided here a small description of each one, so you can refer to the official documentation for the syntax and examples (see https://vuejs.org/api/built-in-components.html):

  • Transition and TransitionGroup are two components that can work together to provide animations and transition to elements and components. They need you to create the CSS animations and transition classes to implement the animation when inserting or removing elements into the page. They are mainly (or often) used when you are displaying a list of elements with v-for/:key or v-if/v-show directives.
  • KeepAlive is another wrapper component (meaning that it surrounds other components) used to preserve the state (internal variables, elements, etc.) when the component wrapped inside is no longer on display. Usually, component instances are cleared out...

Book code conventions

In this book, we will use a set of code conventions and guidelines that are good practices for Vue 3. They will help you not only understand the examples of this book but also the code in the wild that you may come across, as more and more developers use it. Let’s start from the beginning.

Variables and props

These are always in lowercase and spaces are replaced with an underscore, for example, total_count and person_id..

Constants

References to injected objects start with a $ (dollar) sign, for example, $router, $modals, and $notifications.

References to reactive data start with _ and are typed in snake case, for example, _total and _first_input.

References to constant values are all in capital letters, for example, OPTION and LANGUAGE_CODE.

Constructor functions for injected dependencies will start with use, for example, const $store=useStore().

Classes and component names

These are written in PascalCase (each word starts with...

Summary

This chapter has gone from the basics of libraries and frameworks to Vue 3 directives, components, and even code conventions. These concepts are still a bit abstract, so we will bring them down to implementation as we move through the rest of the book and work with real code. However, we are on safe footing now to learn about design principles and patterns in the next chapter.

Review questions

To help you consolidate the contents of this chapter, you can use these review questions:

  • What is the difference between a library and a framework?
  • Why is Vue a “progressive” framework?
  • What are single-file components?
  • What are some of the most common directives used in Vue development?
  • Why are code conventions important?

If you can answer these questions quickly in your mind, you’re good to go! If not, you may want to review the chapter briefly to make sure you have the basis to move on.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn software engineering best practices and design patterns and apply them effectively to your Vue applications
  • Build both SPAs and PWAs using Web Workers and IndexedDB
  • Develop, test, build, and deploy your Vue 3 applications to a real production server

Description

If you’re familiar with the progressive Vue framework for creating responsive user interfaces, you’ll be impressed with its latest iteration, Vue 3, which introduces new concepts and approaches design patterns that are uncommon in other libraries or frameworks. By building on your foundational knowledge of Vue 3 and software engineering principles, this book will enable you to evaluate the trade-offs of different approaches to building robust applications. This book covers Vue 3 from the basics, including components and directives, and progressively moves on to more advanced topics such as routing, state management, web workers, and offline storage. Starting with a simple page, you’ll gradually build a fully functional multithreaded, offline, and installable progressive web application. By the time you finish reading this Vue book, not only will you have learned how to build applications, but you’ll also understand how to solve common problems efficiently by applying existing design patterns. With this knowledge, you’ll avoid reinventing the wheel for every project, saving time and creating software that’s adaptable to future changes.

What you will learn

What is the Vue 3 progressive framework What are software principles and design patterns, how and when to implement them, and the trade-offs to consider Setup your development environment using the new Vite bundler Integrate in your applications state management, routing, multithreading, offline storage, and other resources provided to you by the browser, seldom taken advantage Apply and identify design patterns to solve common problems in the architecture of your web application Best practices for your code, organization, architecture, and user experience implementation Incrementally expand an application with new functionalities without re-writing the whole application each time

What do you get with a Packt Subscription?

Free for first 7 days. ₹800 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details


Publication date : May 30, 2023
Length 296 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781803238074
Category :

Table of Contents

16 Chapters
Preface Chevron down icon Chevron up icon
1. Chapter 1: The Vue 3 Framework Chevron down icon Chevron up icon
2. Chapter 2: Software Design Principles and Patterns Chevron down icon Chevron up icon
3. Chapter 3: Setting Up a Working Project Chevron down icon Chevron up icon
4. Chapter 4: User Interface Composition with Components Chevron down icon Chevron up icon
5. Chapter 5: Single-Page Applications Chevron down icon Chevron up icon
6. Chapter 6: Progressive Web Applications Chevron down icon Chevron up icon
7. Chapter 7: Data Flow Management Chevron down icon Chevron up icon
8. Chapter 8: Multithreading with Web Workers Chevron down icon Chevron up icon
9. Chapter 9: Testing and Source Control Chevron down icon Chevron up icon
10. Chapter 10: Deploying Your Application Chevron down icon Chevron up icon
11. Chapter 11: Bonus Chapter - UX Patterns Chevron down icon Chevron up icon
12. Final words Chevron down icon Chevron up icon
13. Index Chevron down icon Chevron up icon
14. Other Books You May Enjoy Chevron down icon Chevron up icon
Appendix: Migrating from Vue 2 Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(2 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


Wellington Mekhoe Jan 18, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Feefo Verified review Feefo image
Kirill Sakun Dec 12, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Feefo Verified review Feefo image
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.