This blog post was originally written for and posted on Rangleās blog. You can find a link to the original here.
Learning is a unique experience. It requires relating what weāve previously experienced to new concepts and patterns. Those experiences are remembered specifically and each person remembers a concept a little differently. I remember a picture of a ball falling with labelled symbols, others might remember the equation, and some may remember the sounds of the physics hall. Recollection can be a little different and thus, learning requires varied approaches.
Vue is a unique JavaScript framework. Itās unique because it can be approached from several different angles and utilized in a large variety of applications. Where other frameworks provide strong opinions, Vue seems to stay flexible. This flexibility is to the advantage of those trying to learn and master it as they can iteratively learn and extend Vue to deliver more functionality in an accessible way for developers.
It can seem daunting to learn a framework, but Vue helps out bit by providing multiple ways to setup a project and get started. Without exploring all of the options, getting started is difficult. What follows are a series of gateways to learning Vue. These should help align your learning strategy and highlight relevant framework features for getting started:
-
Just bind some data. A great starting point for those new to web applications. Data binding also offers a simple way to prototype out simple webpages that are populated by a Vue application. Online tooling helps to deliver a rich environment for learning the basic building blocks to developing with Vue.
-
Use the vue-cli to generate an project. An introduction point for those familiar with other web development frameworks like React and Angular. This approach uses the CLI and has the added benefit of exposing how Vue is configured for those familiar with build toolchains.
-
Import Vue from a CDN and use standard files. Recommended for those who arenāt familiar with modern framework tooling and build systems but are familiar with JavaScript, HTML, and CSS. A simple import allows full Vue functionality on existing web applications to help explore where Vue can improve things.
-
Setup a vue project with types. TypeScript typings can help those who enjoy working with an IDE to help discover Vueās APIs and learn how to use it organically. Those more comfortable using
class
focused languages and objects will find something familiar on this path.
Skip to the section that sounds most familiar or approachable. Building a base understanding that is familiar will help to learn more about the framework by establishing foundational mental connections. If youāre curious, take a look at the other sections but donāt feel pressured to take in everything.
Just Bind Some Data
Data binding is the process of tying data to an HTML element or attribute. When the data changes, the page updates automatically.
This core frontend concept is an exciting one to use when beginning to learn Vue because it offers a lot of functionality without needing to know how Vue works. Hereās a simple example:
The Vue docs have a good reference for whatās going on here
This code is running on an online editor called CodePen which simply allows experimentation with Vue code. Having something running in a browser environment like this is a great place to start learning because everything is available in one place. CodePen also supports hot-reloading which will re-render the entire page whenever code is changed. This is the perfect playground for getting started with binding because itās very easy to visualize changes.
This understanding of bindings will ignite curiosity. āWhat more can I do with this?ā āI wonder what happens if I change this?ā By understanding how data can be rendered it becomes interesting what might be possible.
This simple curiosity drove me to keep learning. Hereās a rough prototype that I built when I was first learning Vue and was deciding how to render some data for Farmers Markets:
I learned a lot on this example because I just wanted to keep getting something working. First I wanted to use some actual data to populate a list of the markets. Next, I actually wanted to show the details of a market. As a finishing touch I wanted to add a little āroutingā to change the whole view when a market was selected. Each of these steps motivated me to learn what I needed to get the next feature working the way I intended.
With a simple binding introduction, those new to frameworks have everything they need to get started learning more Vue and modern web development:
Generate A Project With The vue-cli
Learning a new framework can be easier with an example code base. As the creatives of history have frequently stated, nothing is more intimidating than a blank page. The vue-cli
can create an application with everything a basic Vue app needs including a webpack build system. The instructions can be found in the Vue docs.
Itās fine to just click through, with āENTERā, on everything selected as default during the setup process. The settings there are extra and arenāt required for getting started.
The CLI has a lot of other advantages. Itās opinionated about folder structure and defines where assets, components, and tests are located. This folder structure can be easily changed, but itās convenient to have the structure setup for us. Using .vue
files, it shows framework users how to structure files and includes all of the required pieces. The Webpack based build system does the work to compile the project and serve it up in a state where changes are automatically reloaded to visualize code changes.
Because itās a full-featured project, those familiar with other frameworks can make connections to how they worked with other frameworks in the past. This builds curiosity. āWhatās different here?ā āI wonder how Vue handles that issueā¦ā āIāve never heard of this featureā¦ā These questions will drive more learning.
Hereās an interactive example of a simple loading bar that I added to a basic Vue template project:
See the Sandbox for Vue Loading Spinner by Ben Hofferber on CodeSandbox.
By having all of the code for a component in one .vue
file, itās easy to see everything at once and to quickly debug and iterate on components. CodeSandbox is great for prototyping out from the vue-cli
base configuration without having to actually install anything locally and it provides a great way to share code.
Here are some things Iāve found interesting that may ignite further curiosity:
Standard Files using a CDN import
Vue can be initialized with a simple import via a JavaScript file over a CDN (Content Delivery Network) in the HTML. This allows users brand new to Vue to experiment with the framework just like any other JavaScript library they might be used to (Bootstrap, JQuery, etc.).
<script src="https://cdn.jsdelivr.net/npm/vue@latest/dist/vue.js"></script>
Once this script is added as an import itās easy to start using it as Vue
now exists on the global namespace. Hereās a simple example of how itās used at its most basic:
The Vue docs have a good reference for whatās going on here. I used CodePen to create this example and CodePen actually uses a CDN to import Vue.
Now there isnāt a lot of code here and itās easy to hook in and start using the framework. Setting up a basic usage of Vue offers good starting point. Developers can expand on their understanding by adding functionality as needs arise. āNow that Iām getting names and variables from JavaScript, how can I display this list?ā āIs there a way to make that appear when a user clicks on this button?ā
This direct application against a problem is a great way to use that curiosity to learn. When going to the Vue docs later, there are now connections that can be built upon to start discovering more functionality that Vue offers.
Hereās a simple HTML and CSS sidemenu prototype that could be expanded:
To start going much farther, Iād need to start manipulating the DOM directly or actually creating multiple pages that can be linked together. To handle links Iād need to stop using this CodePen and move off to a local copy where I can define multiple HTML files to use. There isnāt a big problem with doing that, but Vue can help this prototype expand its functionality quickly.
Now the sidemenu is clickable and the page displays differently depending on what was clicked. The developer is able to iteratively learn and hook into parts of Vueās functionality to handle problems as they arise. By focusing on utilizing Vue as a helper rather than the core of the application, it becomes much more approachable. Being more approachable makes learning easier because it doesnāt seem like such a great challenge to overcome.
Working off of a CDN can limit developers when itās time to ship to production. Having a CDN dependency can be a security vulnerability and the application will be slowed down by having to download the vue-compiler
which is shipped within the CDN. However thereās a lot to learn and a lot that can be accomplished without having to use anything other than JavaScript, CSS, and HTML. Here are some of my favourite framework plugins that can be used from a CDN:
Setup a Typed Vue Environment
TypeScript typings help developers to grow an understanding of a new API organically. By having access to method definitions and the attached docs they can more easily grow an understanding while developing. By being able to see the types involved and by utilizing static type checking, it is also easier to find errors and refactor code. Seeing a new method in the auto-complete list could encourage investigation or perhaps encourage developers to dive into the Vue source code to better understand just what the API is providing.
Vue ships with support for TypeScript 2.6+ typings in its core libraries (vue
, vue-router
, and vuex
). Also, the upcoming @vue/cli
v3 will [fully support TypeScript based projects]((https://vuejs.org/v2/guide/typescript.html#Official-Declaration-in-NPM-Packages) out of the box moving forward. Until then, developers can rely on community projects or a few custom tweaks to get started quickly. Hereās an example of TypeScript enabled on a simple vue-cli
application via .vue
files:
See the Sandbox for Vue TypeScript Example by Ben Hofferber on CodeSandbox.
Itās nice to be able to make just a few changes to enable types. The CLI does a great job of allowing script tags to be flexible by simply adding lang="ts"
. This functionality is made possible by the vue-loader
handling all of the magic in the .vue
files.
To take full advantage of the typings itās important to use an editor that provides good support for bindings. A good choice is Visual Studio Code which has great TypeScript support out of the box. Additionally, the plugin Vetur is handy for enabling Vue specific language features within the editor.
With a great environment setup and a few simple configurations to setup TypeScript, developers are ready to start exploring the API with types organically. Iāll leave you with a few additions for type-savy developers who are interested in more aspects of Vue: