Vue.js Quick Start Guide
eBook - ePub

Vue.js Quick Start Guide

Learn how to build amazing and complex reactive web applications easily using Vue.js

  1. 186 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Vue.js Quick Start Guide

Learn how to build amazing and complex reactive web applications easily using Vue.js

About this book

Learn and explore all important features of Vue.js through a number of simple examples.

Key Features

  • Uses latest features such as Vue-cli 3, Vuex, and Nuxt
  • Practical examples to understand Vue 2 quickly
  • Step-by-step approach to reinforce concepts covered

Book Description

Vue.js is the latest trending frontend framework. Simplicity, reactivity, and flexibility are some of the key benefits that Vue offers to developers. This book will help you learn everything you need to know to build stunning reactive web apps with Vue.js 2 quickly and easily.

This book will take you through the Vue 2 framework. You will start by learning the different Vue installation options: CDN, NPM, and Vue CLI. Then we will look at the core concepts of Vue: templates and components – ways to modularize Vue code. You will learn how to utilize directives, which are Vue-specific HTML attributes with additional features. Also, you will see how Vue uses a streamlined approach to development, with reusable methods, computed properties, and watchers, and how it controls state with the help of its data option.

You will learn about the concepts of reactive programming in Vue, and how to understand communication between parent and child components. We will take a look at props and slots, working with CSS, filters, and mixins. We will also look at ways to add transitions and animations to Vue apps. Then you will extend Vue by building custom directives and your own plugins.

Finally, you will learn about Vuex – a Vue plugin that allows us to centralize state, and also introduce Nuxt, which is a framework that builds on top of Vue and solves some issues of single-page applications. After learning about these components, you will be ready to build your own reactive web apps with Vue.js 2.

What you will learn

  • Develop apps with Vue.js
  • Reuse components using slots
  • Use filters, mixins, and global mixins in Vue
  • Build custom directives in Vue
  • Work with CSS animations
  • Work with templates, directives, methods, data, computed properties, and watchers
  • Use Nuxt and Vue-Router
  • Build and deploy an SSR Vue app

Who this book is for

This book is for people who want to learn and experience developing with Vue.js. Familiarity with HTML, CSS, and JavaScript will help you get the most from this book.

Trusted by 375,005 students

Access to over 1.5 million titles for a fair monthly price.

Study more efficiently using our study tools.

Information

Year
2018
Print ISBN
9781789344103
Edition
1
eBook ISBN
9781789344172

Working with Vue-CLI, Components, Props, and Slots

The previous chapter was an introduction to the basic concepts of Vue. We will start this chapter with a more realistic approach: we'll introduce Vue-cli. We'll look at the component hierarchy, global and local components, and communication between components. We will introduce slots, and we will also examine the difference between slots and props.
In this chapter, we will cover the following topics:
  • Vue component hierarchy, and global and local components
  • Using Vue-cli
  • Setting up code editors to use with Vue
  • The structure of our Vue-cli-based project
  • Adding basic functionality to a child component
  • Adding props to our HelloAgain.vue
  • Introduction to slots

Vue component hierarchy, and global and local components

As we learned in Chapter 2, Basic Concepts of Vue 2, to get a new Vue instance running, we use new Vue:
new Vue(
el: "#app",
// the rest of the Vue instance code here
)
Our app component resides inside this Vue instance.
The app component usually has a child component, like we saw in this example from Chapter 2, Basic Concepts of Vue 2: https://codepen.io/AjdinImsirovic/pen/xzpOaJ:
Vue.component('custom-article', {
template: `
<article>
Our own custom article component!
</article>`
})
new Vue({
el: '#app'
})
What we did not mention in the previous chapter is this:
  • A child component can be reused as many times as needed
  • A child component can also have its own children
An example of this is available in the following pen: https://codepen.io/AjdinImsirovic/pen/ZjdOdK.
Here is the code which demonstrates these two principles:
// JS
Vue.component('custom-article', {
template: `
<article>
Our own custom article component!
</article>`
})
Vue.component('another-custom-article', {
template: `
<article>
Another custom article component!
This one has it's own child component too!
Here it is:
<custom-article></custom-article>
</article>`
})
new Vue({
el: '#app'
})

/* CSS */
article {
font-size: 40px;
padding: 20px;
color: limegreen;
font-family: Arial;
border: 3px solid green;
border-radius: 10px;
}

<!-- HTML -->
<main id="app">
<custom-article></custom-article>
<custom-article></custom-article>
<another-custom-article></another-custom-article>
</main>
As seen already, to add a component to our Vue instance, we are using the following syntax:
Vue.component('another-custom-article', { // etc...
In Vue terminology, we use this code to register a component. As described before, it's referred to as global registration. There is also local registration.
Local registration works similarly to the Vue.component syntax. The only difference in the code is how we introduce the local component when compared to a global one. In the previous code, we had the following global component:
Vue.component('custom-article', {
template: `
<article>
Our own custom article component!
</article>`
})
Converting this global component to a local component is as simple as removing this snippet of code:
Vue.component('custom-article'
Instead of the previous code, we'll simply make a new variable and give it the exact same options object that we used in the global component, like this:
var customArticle = {
template: `
<article>
Our own custom article component!
</article>`
}
In order to use this local component in our Vue instance, we'll introduce the components option, like this:
new Vue({
el: '#app',
components: {
'custom-article': customArticle
}
})
An example with a local component is available here: https://codepen.io/AjdinImsirovic/pen/ZMzrpr.
However, the previous example is incomplete on purpose. As we can see, the customArticle local component is only available in the main Vue instance, but it is not available in the anotherCustomArticle component.
To make this work and complete the example, we need to tweak this bit of code:
Vue.component('another-custom-article', {
template: `
<article>
Another custom article component!
This one has it's own child component too!
Here it is:
<custom-article></custom-article>
</article>`,
//components: {
// 'customArticle': customArticle
//}
})
We will simply remove the comments on these three lines:
 components: {
'customArticle': customArticle
}
By doing that, we have registered the local component customArticle in the global component anotherCustomArticle. Basically, we are following the same procedure of registering a local component in our main Vue instance, and we are applying that same approach of registering local component in our anotherCustomArticle global component.
To get into the nuances of global and local registration, you can refer to this section of the official Vue documentation:
https://vuejs.org/v2/guide/components-registration.html.
In the following section, we'll start using Vue-cli.

Using Vue-CLI

In order to start using Vue-cli, we need to have Node.js set up on our machine, and we also need to have a command-line app on our operating system of choice.
For example, my preferred tools are Windows 10 and Git bash for Windows.
There are many different operating systems and command-line apps that you could potentially be using.

If you run into problems during the installation of any of the tools mentioned in this section, it might be worthwhile to have a look at this in-depth guide on installing Node.js on your operating system:

https://www.packtpub.com/mapt/book/web_development/9781788626859/2

Installing Git bash

You first need to visit https://git-scm.com/downloads, which lets you choose between macOS X, Windows, and Linux/Unix installations. After clicking on the Windows download, you can proceed with the installation steps for Git bash. Just following the default preset options during the installation should be fine.

Installing nvm

To download the Node version manager for Windows, visit this link:
https://github.com/coreybutler/nvm-windows/releases
Once on the page, click the nvm-setup.zip file to download it, then run the downloaded nvm-setup.exe and go through the regular installation steps.
Next, start Git bash with administrator privileges and run the following command:
nvm install 8.11.4
The following message will be logged to the console:
Down...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Introducing Vue
  7. Basic Concepts of Vue 2
  8. Working with Vue-CLI, Components, Props, and Slots
  9. Filters and Mixins
  10. Making Your Own Directives and Plugins
  11. Transitions and Animations
  12. Using Vuex
  13. Using Nuxt.js and Vue-Router
  14. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.5M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1.5 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Vue.js Quick Start Guide by Ajdin Imsirovic in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in JavaScript. We have over 1.5 million books available in our catalogue for you to explore.