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
$31.99 $21.99
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
$31.99 $21.99
Print
$39.99
Subscription
$15.99 Monthly
eBook
$31.99 $21.99
Print
$39.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon AI Assistant (beta) to help accelerate your learning
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

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 eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon AI Assistant (beta) to help accelerate your learning
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.