Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Jump Start Vue.js
Jump Start Vue.js
Jump Start Vue.js
Ebook215 pages2 hours

Jump Start Vue.js

Rating: 4 out of 5 stars

4/5

()

Read preview

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!
LanguageEnglish
PublisherSitePoint
Release dateSep 13, 2021
ISBN9781098129514
Jump Start Vue.js
Author

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

Related to Jump Start Vue.js

Related ebooks

Programming For You

View More

Related articles

Reviews for Jump Start Vue.js

Rating: 4 out of 5 stars
4/5

1 rating0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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 directory

    As 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:

    main>

     

    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

    Enjoying the preview?
    Page 1 of 1