Vue routing lazy loading details

Vue routing lazy loading details

1. What is lazy loading of routes?

Official explanation:

  • When bundling your app, the JavaScript bundle can become very large, impacting page load.
  • It would be more efficient if we could split the components corresponding to different routes into different code blocks, and then load the corresponding components when the route is accessed.

What the official meant

  • First of all, we know that there are usually many different pages defined in the route
  • Where will this page be packaged in the end? Generally, it will be placed in a js file
  • But with so many pages, putting all the files in one js file will inevitably make the page very large.
  • If we request this page from the server all at once, it may take some time, and there may even be a brief blank on the user's computer.
  • How to avoid this? Use lazy loading of routes

What does lazy loading of routes do?

The main function of lazy loading of routes is to package the components corresponding to the routes into js code blocks, and only load the corresponding components when the route is accessed.

2. Use of lazy loading of routes

Before using it, let's take a look at how the original code loads the route

import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/Home";
import About from "@/views/About";
import User from "@/views/User";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/about",
    name: "About",
    component: About
  },
  {
    path: "/user/:userId",
    name: "User",
    component: User
  }
];

We can see that we have imported the components corresponding to the route from the beginning. If there are many components that need to be imported, the loading page will be relatively slow. Let's take a look at the files packaged in this way.

We can see that there are only two js files packaged in this way. When we load the page later, we need to load all the two files before the page will be displayed. If the amount of code is too much, the page response will be slow, which will give a very bad user experience.

Next we use lazy loading of routes

import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

// Add new route lazy loading code const Home = () => import('../views/Home')
const About = () => import('../views/About')
const User = () => import('../views/User')

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/about",
    name: "About",
    component: About
  },
  {
    path: "/user/:userId",
    name: "User",
    component: User
  }
];

We can see that nothing needs to be changed in the routing configuration. Just use it as usual. Just declare a variable before that and use the arrow function in the variable to import the corresponding component. It is very simple to use.

The file structure packaged using lazy loading of routes is as follows:

We can see that there are 3 more js files than the original method. This is because the 3 components in our code above use route lazy loading. These 3 js files will only be loaded when the route is accessed, which can save a lot of loading time.

Therefore, we recommend using lazy loading to load routes.

This is the end of this article about the details of vue routing lazy loading. For more related vue routing lazy loading content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue-router routing lazy loading and 3 ways to implement it
  • Examples of 3 ways to implement lazy loading of routes in Vue
  • How to name Webpack Chunks in Vue routing lazy loading
  • How to implement lazy loading of routes and components in Vue
  • Implementation of vue-router routing lazy loading (solving the slow first loading of vue project)
  • Analysis of the principle of using import for lazy loading of routes in Vue

<<:  Detailed explanation of Shell script control docker container startup order

>>:  How to start multiple MySQL databases on a Linux host

Recommend

Detailed tutorial on running selenium+chromedriver on the server

1. Introduction I want to use selenium to scrape ...

JavaScript to implement the countdown for sending SMS

This article shares the specific code of JavaScri...

Setting the engine MyISAM/InnoDB when creating a data table in MySQL

When I configured mysql, I set the default storag...

Detailed troubleshooting of docker.service startup errors

Execute the following command to report an error ...

Introduction to the use of the indeterminate property of the checkbox

When we use the folder properties dialog box in Wi...

In-depth understanding of the use of r2dbc in MySQL

Introduction MySQL should be a very common databa...

How to install Maven automatically in Linux continuous integration

Unzip the Maven package tar xf apache-maven-3.5.4...

Vue realizes the function of uploading photos on PC

This article example shares the specific code of ...

Document Object Model (DOM) in JavaScript

Table of contents 1. What is DOM 2. Select elemen...

Comparison of two implementation methods of Vue drop-down list

Two implementations of Vue drop-down list The fir...

Detailed explanation of html printing related operations and implementation

The principle is to call the window.print() metho...

A Different Kind of "Cancel" Button

The “Cancel” button is not part of the necessary ...