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

Mini Program to implement Token generation and verification

Table of contents process Demo Mini Program Backe...

SQL implementation of LeetCode (177. Nth highest salary)

[LeetCode] 177.Nth Highest Salary Write a SQL que...

Web Design Tutorial (8): Web Page Hierarchy and Space Design

<br />Previous article: Web Design Tutorial ...

Vue3+TypeScript implements a complete example of a recursive menu component

Table of contents Preface need accomplish First R...

Example code of setting label style using CSS selector

CSS Selectors Setting style on the html tag can s...

A brief discussion on Vue3 father-son value transfer

Table of contents From father to son: 1. In the s...

A brief discussion on how to customize the host file in Docker

Table of contents 1. Command 2. docker-compose.ym...

Detailed explanation of two points to note in vue3: setup

Table of contents In vue2 In vue3 Notes on setup ...

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...

Swiper.js plugin makes it super easy to implement carousel images

Swiper is a sliding special effects plug-in built...

JS implementation of carousel example

This article shares the specific code of JS to im...

CSS achieves colorful and smart shadow effects

background Ever wondered how to create a shadow e...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...