Welcome to Full-Stack Vue.js 2 and Laravel 5! In this first chapter, we'll take a high-level overview of Vue.js, getting you familiar with what it can do, in preparation for learning how to do it.
We'll also get acquainted with Vuebnb, the main case-study project featured in this book.
Topics this chapter covers:
- Basic features of Vue, including templates, directives, and components
- Advanced features of Vue including single-file components and server-side rendering
- Tools in the Vue ecosystem including Vue Devtools, Vue Router, and Vuex
- The main case-study project that you'll be building as you progress through the book, Vuebnb
- Instructions for installing the project code
At the time of writing in late 2017, Vue.js is at version 2.5. In less than four years from its first release, Vue has become one of the most popular open source projects on GitHub. This popularity is partly due to its powerful features, but also to its emphasis on developer experience and ease of adoption.
The core library of Vue.js, like React, is only for manipulating the view layer from the MVC architectural pattern. However, Vue has two official supporting libraries, Vue Router and Vuex, responsible for routing and data management respectively.
Vue is not supported by a tech giant in the way that React and Angular are and relies on donations from a small number of corporate patrons and dedicated Vue users. Even more impressively, Evan You is currently the only full-time Vue developer, though a core team of 20 more developers from around the world assist with development, maintenance, and documentation.
The key design principles of Vue are as follows:
- Focus: Vue has opted for a small, focused API, and its sole purpose is the creation of UIs
- Simplicity: Vue's syntax is terse and easy to follow
- Compactness: The core library script is ~25 KB minified, making it smaller than React and even jQuery
- Speed: Rendering benchmarks beat many of the main frameworks, including React
- Versatility: Vue works well for small jobs where you might normally use jQuery, but can scale up as a legitimate SPA solution
Let's now do a high-level overview of Vue's basic features. If you want, you can create an HTML file on your computer like the following one, open it in your browser, and code along with the following examples.
If you'd rather wait until the next chapter, when we start working on the case-study project, that's fine too as our objective here is simply to get a feel for what Vue can do:
Although Vue can be used as a JavaScript module in more sophisticated setups, it can also simply be included as an external script in the body of your HTML document:
By default, Vue will use an HTML file for its template. An included script will declare an instance of Vue and use the el
property in the configuration object to tell Vue where in the template the app will be mounted:
We can bind data to our template by creating it as a data
property and using the mustache syntax to print it in the page:
Similar to Angular, we can add functionality to our templates by using directives. These are special properties we add to HTML tags starting with the v-
prefix.
Say we have an array of data. We can render this data to the page as sequential HTML elements by using the v-for
directive:
A key feature of Vue's design is its reactivity system. When you modify data, the view automatically updates to reflect that change.
For example, if we create a function that pushes another item to our array of grocery items after the page has already been rendered, the page will automatically re-render to reflect that change:
Two seconds after the initial rendering, we see this:
Components extend basic HTML elements and allow you to create your own reusable custom elements.
For example, here I've created a custom element, grocery-item
, which renders as a li
. The text child of that node is sourced from a custom HTML property, title
, which is accessible from within the component code:
This renders as follows:
But probably the main reason to use components is that it makes it easier to architect a larger application. Functionality can be broken into reuseable, self-contained components.