Build A Lazy-Load Router With Vue.js And The Latest Browser Features

Build A Lazy-Load Router With Vue.js And The Latest Browser Features

Dynamic module importing is one of the latest JavaScript features to hit the major browsers. The main use case for this feature is lazy-loading modules to allow content to be delivered when it is needed, rather than all at once.

In this article, I’ll demonstrate how you can build a lazy-load router with Vue.js in just a few lines of code. This will work natively in browsers that have implemented dynamic module imports, but I’ll also include a fallback for older browsers.

Grab the finished code here on Github.

Static JavaScript module imports

If you’re using the latest version of any major browser, you can perform static import/export natively now. For Vue.js, this means you can export a component definition from a file like this:

BooksPage.js

export default {
  name: 'BooksPage',
  template: `
    <div>
     <h1>Books Page</h1>
     <p></p>
    </div>
  `,
  data() {
    return {
      message: 'Oh hai from the books page'
    }
  }
};

And import it in your app like this:

read more

Build A Collapsible Tree Menu With Vue.js Recursive Components

Build A Collapsible Tree Menu With Vue.js Recursive Components

A recursive component in Vue.js is one which invokes itself e.g.:

Vue.component('recursive-component', {
  template: `<!--Invoking myself!-->
             <recursive-component></recursive-component>`
});

Recursive components are useful for displaying comments on a blog, nested menus, or basically anything where the parent and child are the same, albeit with different content. For example:

To give you a demonstration of how to use recursive components effectively, I’ll go through the steps of building an expandable/contractable tree menu. Here’s the finished product in a Codepen, try it out:

See the Pen Vue.js Expandable Tree Menu (Recursive Components) by Anthony (@anthonygore) on CodePen.

read more

Build A Real-Time Chat App With VueJS, Vuex & Cloud Firestore

Build A Real-Time Chat App With VueJS, Vuex & Cloud Firestore

Last week Google’s Firebase platform released a new product, Cloud Firestore. Much like the Firebase real-time database, it is a NoSQL-based cloud database that can be used to build real-time applications. It addresses some of the problems Firebase has, like writing complex queries. You can read more about its features here.

In this post, I’ll be building a simple real-time chat application called Firechat which uses Vue and Vuex, and the new Cloud Firestore. I’ll look at how to integrate Firestore into a Vue.js application and some best practices with Vuex. You can get the final source code here on Github.

Installation

Let’s start by scaffolding a new Vue application using the vue-cli. I’ll use the webpack-simple template.

$ vue init webpack-simple firechat

Before going any further, I want to use the spread operator ... and async/await. We will also be using Vuex for state management, and uuid for generating random ids, so let’s install those. We will also need the Firebase module.

$ npm install babel-plugin-transform-async-to-generator babel-plugin-transform-object-rest-spread Firebase babel-polyfill vuex uuid --save

Next, in the project root directory, edit .babelrc like so:

{
  "presets": [
    ["env", { "modules": false }]
  ],
  "plugins": ["transform-async-to-generator", "transform-object-rest-spread"]  
}

Now we can use the spread operator ... and await/async.

Before going any further, go to Firebase and sign up if you haven’t got an account. Once you do, click “Add Project” and enter a name.

Then click “Add Firebase to your web app”.

read more

Offline First Masonry Grid Showcase with Vue

Offline First Masonry Grid Showcase with Vue

To keep your product revelant in the market, you should be building Progressive Web Apps (PWA). Consider these testimonies on conversion rates, provided by leading companies, such as Twitter, Forbes, AliExpress, Booking.com and others. This article doesn’t go into background, history or principles surrounding PWA. Instead we want to show a practical approach to building a progressive web app using the Vue.js library.

Here is a breakdown of the project we will be tackling:

  • A masonry grid of images, shown as collections. The collector, and a description, is attributed to each image. This is what a masonry grid looks like:
  • An offline app showing the grid of images. The app will be built with Vue, a fast JavaScript framework for small- and large-scale apps.
  • Because PWA images need to be effectively optimized to enhance smooth user experience, we will store and deliver them via Cloudinary, an end-to-end media management service.
  • Native app-like behavior when launched on supported mobile browsers.

Let’s get right to it!

Setting up Vue with PWA Features

A service worker is a background worker that runs independently in the browser. It doesn’t make use of the main thread during execution. In fact, it’s unaware of the DOM. Just JavaScript.

read more

Getting Your Head Around Vue.js Scoped Slots

Getting Your Head Around Vue.js Scoped Slots

Scoped slots are a useful feature of Vue.js that can make components more versatile and reusable. The only problem is they’re difficult to understand! Trying to get your head around the interweaving of parent and child scopes is like solving a tough math equation.

A good approach when you can’t understand something easily is to try put it to use in solving a problem. In this article, I’ll demonstrate how I used scoped slots to build a reusable list component.

Shape and color list

Note: You can see the finished product in this Codepen.

The basic component

The component we’re going to build is called my-list and it displays lists of things. The special feature is that you can customize how the list items are rendered in every usage of the component.

Let’s tackle the simplest use case first, and get my-list to render just one list of things: an array of geometric shape names and the number of sides they have.

app.js

Vue.component('my-list', {
  template: '#my-list',
  data() {
    return {
      title: 'Shapes',
      shapes: [ 
        { name: 'Square', sides: 4 }, 
        { name: 'Hexagon', sides: 6 }, 
        { name: 'Triangle', sides: 3 }
      ]
    };
  }
});

new Vue({
  el: '#app'
});

index.html

<div id="app">
  <my-list></my-list>
</div>

<script type="text/x-template" id="my-list">
  <div class="my-list">
    <div class="title">{{ title }}</div>
    <div class="list">
      <div class="list-item" v-for="shape in shapes">
        <div>{{ shape.name }} <small>({{ shape.sides }} sides)</small></div>
      </div>
    </div>
  </div>
</script>

read more