Author -  Sai gowtham

How to implement Lazy loading in Vue router

Lazy loading helps us to split our code into chunks and load that chunk when a route is activated.

Getting started

First, we are creating a new vue project by using vue-cli.

vue create vue-lazy

This above command will download vue-related files into vue-lazy folder.

Change your working directory by running the following command.

cd vue-lazy

Now, open vue-lazy folder in your favorite code editor.

Creating components

Let’s create three new components inside our components folder so that we can create routing.

Home.vue
<template>
  <div>
    <h1>This is Home page</h1>
  </div>
</template>
User.vue
<template>
  <div>
    <h1>This is User page</h1>
  </div>
</template>
Contact.vue
<template>
  <div>
    <h1>This is Contact page</h1>
  </div>
</template>

Installing vue router

Let’s install the vue router package from the npm.

npm i vue-router

Creating Lazy routes

For lazy loading, we need to use the dynamic import syntax instead of normal imports.

Open your main.js replace it with below code.

main.js
import Vue from 'vue'
import App from './App.vue';
import VueRouter from "vue-router";
import Home from './components/Home.vue';

 // dynamic imports
const User = () => import('./components/User.vue');const Contact = () => import('./components/Contact.vue');
Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  routes: [
    { path: '/', component: Home },
    {path: '/user', component: User},
    {path: '/contact', component: Contact}
  ]
})

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

Adding Navigation

App.vue
<template>
  <div id="app">
    <ul class="nav">
      <router-link to="/">Home</router-link>
      <router-link to="/user">User</router-link>
      <router-link to="/contact">Contact</router-link>
    </ul>
    <router-view></router-view>
  </div>
</template>

<script></script>

<style>
a {
  margin-left: 1rem;
}
</style>

Let’s test our vue app in the browser by opening network panel.

vue-lazy-loading-example

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY