Home > Web Front-end > JS Tutorial > Detailed explanation of vue-router lazy loading (with code)

Detailed explanation of vue-router lazy loading (with code)

php中世界最好的语言
Release: 2018-04-20 11:24:21
Original
2463 people have browsed it

This time I will bring you a detailed explanation of vue-router lazy loading (with code). What are the precautions when using vue-router lazy loading? The following is a practical case, let's take a look.

Everyone who has used vue-router knows that it can implement lazy loading of module js, that is, only load the js script file of the corresponding module when needed to speed up the display of the homepage. For example, only when the user clicks a certain "User Information" button or menu for the first time, the user will click

. This time I will bring you what are the precautions. The following is a practical case, let's take a look.

Load the js component of the "User Information" module.

The implementation of lazy loading relies on the function of AMD mode require function under webpack. Webpack will generate an independent js file from the asynchronous require file. When calling, it will download the js asynchronously and execute it after completion. The key code implemented in the development project is:

const basicInfo = {
  path: '/user',
  component: resolve => require(['./basicInfo.vue'], resolve) 
}
//然后将这个basicInfo加入路由表中
Copy after login

But there is a problem here: starting from the user clicking the "User Information" menu, to the completion of js file download , due to downloading from the network js has a time delay, during which the user interface does not respond at all, making users feel that clicking on it is ineffective, and they often click again. This is especially true when the js file is large and the network speed is slow. Therefore, it is necessary to add a Loading loading prompt in this process.

We analyze this line of code:

resolve => require(['./basicInfo.vue'], resolve)
Copy after login

It is a function that executes the require process, and then calls resolvecallback function after it is completed. We only need to encapsulate it, display Loading before require is executed, and then hide Loading when the callback is completed and executed, and this requirement will be achieved. As follows:

const basicInfo = {
  path: '/user',
  component: resolve => {
    [显示Loading]
    require(['./basicInfo.vue'], component => {
      [隐藏Loading]
      resolve(component)
    })
  }
};
Copy after login

The code for displaying and hiding Loading can be processed according to your own UI framework. For example, element-ui:

import { Loading } from 'element-ui';
var unique;
export default {
  show() {
    let opt = {body: true, text: 'Loading...'};
    if(!unique) unique = Loading.service(opt);
  },
  resolve(resolve) {
    return function (component) {
      if (unique) {
        unique.close();
        unique = null;
      }
      resolve(component)
    }
  }
}
const basicInfo = {
  path: '/user',
  component: resolve => {
    spinRoute.show();
    require(['./basicInfo.vue'], spinRoute.resolve(resolve))
  }
};
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to upgrade vue cli to webapck4

vue-cli 3.0 Beginners must know

D3.js realizes dynamic dial effect

The above is the detailed content of Detailed explanation of vue-router lazy loading (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template