Jump Start Vue.js
4/5
()
About this ebook
Since its release in 2014, Vue.js has seen a meteoric rise to popularity and is now considered one of the primary front-end frameworks, and not without good reason. Its component-based architecture was designed to be flexible and easy to adopt, making it just as easy to integrate into projects and use alongside non-Vue code as it is to build complex client-side applications.
Now revised to cover Vue 3, this short book is ideal for front-end developers who need a rapid introduction to Vue.js. It covers:
- Basic concepts: Vue fundamentals, templates, and reactive data
- Components: custom components, events and slots
- State management: Vuex, mutations, actions
- Routes: creating routes, links and route guards
- Nuxt.js: build sophisticated apps in no time
- And much more!
Nilson Jacques
Nilson is a full-stack web developer who has been working with computers and the web for over a decade. A former hardware technician and network administrator, Nilson is now currently co-founder and developer of a company developing web applications for the construction industry. You can also find Nilson on the SitePoint Forums as a mentor.
Read more from Nilson Jacques
Node.js: Tools & Skills Rating: 0 out of 5 stars0 ratingsVue.js: 11 Practical Projects Rating: 0 out of 5 stars0 ratingsWorking with Vue.js Rating: 0 out of 5 stars0 ratingsLearn Vue.js: The Collection Rating: 0 out of 5 stars0 ratingsYour First Week With Node.js Rating: 0 out of 5 stars0 ratings
Related to Jump Start Vue.js
Related ebooks
JavaScript: Best Practice Rating: 0 out of 5 stars0 ratingsExploring Web Components: Build Reusable UI Web Components with Standard Technologies (English Edition) Rating: 0 out of 5 stars0 ratingsVue.js for Jobseekers: A complete guide to learning Vue.js, building projects, and getting hired (English Edition) Rating: 0 out of 5 stars0 ratingsSvelte: A Beginner's Guide Rating: 0 out of 5 stars0 ratingsMastering JavaScript Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsNode.js: Novice to Ninja Rating: 0 out of 5 stars0 ratingsReact Projects: Build 12 real-world applications from scratch using React, React Native, and React 360 Rating: 0 out of 5 stars0 ratingsWeb Development with MongoDB and NodeJS - Second Edition Rating: 0 out of 5 stars0 ratingsVue.js: Tools & Skills Rating: 0 out of 5 stars0 ratingsReact.js Essentials Rating: 4 out of 5 stars4/5React.js Design Patterns: Learn how to build scalable React apps with ease (English Edition) Rating: 0 out of 5 stars0 ratingsVisual Studio Code: End-to-End Editing and Debugging Tools for Web Developers Rating: 0 out of 5 stars0 ratingsBuilding Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsRails: Novice to Ninja: Build Your Own Ruby on Rails Website Rating: 4 out of 5 stars4/5Understanding JavaScript Promises Rating: 0 out of 5 stars0 ratingsJavaScript: Best Practices to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsA Pocket Guide to CSS Animations Rating: 5 out of 5 stars5/5JavaScript: Tips and Tricks to Programming Code with Javascript Rating: 0 out of 5 stars0 ratingsJump Start PHP Environment: Master the World's Most Popular Language Rating: 0 out of 5 stars0 ratingsCSS Master Rating: 0 out of 5 stars0 ratingsLearning Android Game Development Rating: 0 out of 5 stars0 ratingsJavaScript Projects for Kids Rating: 0 out of 5 stars0 ratingsResponsive Media in HTML5 Rating: 0 out of 5 stars0 ratingsReact Components Rating: 0 out of 5 stars0 ratingsNode.JS Guidebook: Comprehensive guide to learn Node.js Rating: 0 out of 5 stars0 ratingsAdvanced Web Development with React: SSR and PWA with Next.js using React with advanced concepts Rating: 0 out of 5 stars0 ratingsNode Web Development, Second Edition Rating: 0 out of 5 stars0 ratingsReactive Programming for .NET Developers Rating: 0 out of 5 stars0 ratingsJavaScript Application Design: A Build First Approach Rating: 0 out of 5 stars0 ratings
Programming For You
Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsPython Machine Learning By Example Rating: 4 out of 5 stars4/5Unreal Engine from Zero to Proficiency (Foundations): Unreal Engine from Zero to Proficiency, #1 Rating: 3 out of 5 stars3/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5
Reviews for Jump Start Vue.js
1 rating0 reviews
Book preview
Jump Start Vue.js - Nilson Jacques
Chapter 1: Vue.js: The Basics
Since Facebook introduced React in 2013, component-based UI frameworks have become increasingly popular. They allow you to build your applications as collections of self-contained, reusable components that can manage their own state and can be declaratively composed into more complex interfaces.
The second generation of Google’s Angular framework is based around the component paradigm, and a number of smaller, light-weight libraries such as Hyperapp have cropped up in recent years.
Amidst this seemingly ever-growing collection of frameworks and libraries came Vue.js (referred to simply as Vue from here on). Released in 2014, Vue was created by a Google engineer called Evan You, who was inspired to create it after having worked with AngularJS.
Vue has seen a meteoric rise in popularity and is now considered one of the main front-end frameworks, and not without good reason. It was designed to be flexible and easy to adopt, making it just as easy to integrate into projects and use alongside non-Vue code as it is to build complex client-side applications.
Why Choose Vue?
If you’re reading this, you’ve already decided that Vue is worth a look, but there are plenty of good reasons to choose it for your next project.
It’s easy to sprinkle onto
an existing site. You can start using Vue on existing websites without having to implement a build step and introduce a new setup and tooling to your workflow. In fact, Vue is the perfect go-to tool for many situations where you’d previously have reached for jQuery!
Vue’s component-based architecture. Of course, Vue is also more than capable of building modern single-page applications (SPAs), allowing you to develop your apps as modular, reusable components.
Its comprehensive ecosystem. Pre-built components for almost every conceivable use are available from Vue’s large community of developers. On top of that, popular libraries exist to provide common functionality such as client-side routing, state management, and server-side rendering! Many of these are also maintained by the official Vue team, in contrast to React, where there aren’t always official solutions available.
It’s widely used. Vue is being used by a diverse range of businesses, from GitLab to the Chinese giant Alibaba. It’s also been adopted by the PHP framework Laravel as its default library for building client-side apps. It’s safe to say that these organizations consider Vue to be a sensible choice with good future prospects. Its popularity also means that it’s easy to get help with your problems on various support sites and forums.
Getting Started
In order to get to know Vue and see how it works, let’s walk through the basics. To get a feel for what we can do with the framework, we’re going to start with a static HTML page and add in some basic interactivity with Vue.
A basic staff directoryAs an example, we’re going to use this fictional employee directory, which you can find as a sandbox demo at CodeSandbox.io.
The Vue Instance
Let’s start by loading the Vue library from a CDN. For simple use cases, like adding some interactivity to existing websites, this will get us up and running quickly without having to introduce a build step:
Development vs Production
We’re including the development version, which outputs helpful warnings in the console to help you avoid common pitfalls, and integrates with the Vue devtools (which we’ll cover later). In production, you’ll want to switch to the minified version vue.global.prod.js, which is approximately 42KB gzipped.
Next, we need to create a new Vue instance:
Vue.createApp({}).mount('#main');
So far, all we’ve done is tell Vue that we want to create an instance and have it manage an area of the DOM defined by the #main selector. It will use this area of the page as a template.
It will parse this chunk of HTML, looking for expressions that are part of the template language (which we’ll look at shortly) and binding them to our instance data where applicable.
Reactive Data
To be able to do something useful with our Vue instance, we need to give it some data. We do this by defining a function called data(), which returns an object containing all the data we want Vue to work with.
Any properties that are assigned to the returned object (including nested objects and arrays) become reactive, meaning that Vue will observe them and automatically re-render the UI when they change.
Let’s add some example data to our instance:
Vue.createApp({
data() {
return {
heading: Staff Directory
,
employees: [
{
firstName
: amelia
,
lastName
: austin
,
photoUrl
: https://randomuser.me/api/portraits/thumb/women/9.jpg
,
email
: [email protected]
,
phone
: (651)-507-3705
,
department
: Engineering
},
{
firstName
: bobbie
,
lastName
: murphy
,
photoUrl
: https://randomuser.me/api/portraits/thumb/women/79.jpg
,
email
: [email protected]
,
phone
: (925)-667-7604
,
department
: Management
}
]
};
}
}).mount('#main');
Always Declare Your Data Properties
It may be that some of the data you want to work with won’t be available to your instance when you initialize it (if it’s loaded via an Ajax request, for example). It’s good practice to declare initial values for all your data properties, as properties added later won’t be reactive by default.
At this stage, our instance has all the data it needs, but it won’t be able to render anything without a template. Let’s look at how to create one next.
Template Syntax
So now we have our Vue instance, we’ve given it some data, and we’ve told it which part of the page it should be looking at for its template. How do we render our data?
Interpolations
Interpolations are the way that we insert dynamic values into a template. We use the curly brace syntax (aka the Mustache syntax) to output primitive values from our instance data, such as numbers and strings:
ui center aligned header>{{ heading }}
You can also use any valid JavaScript statement between double braces, which Vue will evaluate before rendering.
Here’s the code:
The price is: {{ price * 1.20 }} (inc. VAT)
Vue.createApp({
data() {
return {
price: 25
}
}
}).mount('#main');
And here’s the output:
The price is: £30 (inc. VAT)
In this example, we’re performing a calculation with the data price before outputting it. The calculation is just a normal JavaScript expression.
Directives
Of course, to build any sort of interesting UI, we need to do more than just display simple values. Vue’s template syntax includes directives for looping and conditional display logic, as well as binding HTML attributes to reactive data properties. Directives are attributes you add to DOM elements and components (very similar to AngularJS’s ng-* directives).
v-for
Thinking back to our example instance data, you’ve probably already wondered how we go about looping over collections of data such as arrays. The v-for directive allows us to tell Vue that we want a section of our template to be rendered for every item in a collection (which can be an array or an object):
employee in employees>
https://randomuser.me/api/portraits/thumb/women/9.jpg
class=ui mini rounded image
/>
{{ employee.firstName }}
{{ employee.lastName }}
{{ employee.email }}
{{ employee.phone }}
{{ employee.department }}
Here, the element with the v-for directive, and all its child elements, will be repeated for each employee in the array. You might be wondering why the image src is