Home > Web Front-end > JS Tutorial > How to deal with the slow speed caused by too many resources being loaded for the first time during lazy loading of vue-router

How to deal with the slow speed caused by too many resources being loaded for the first time during lazy loading of vue-router

php中世界最好的语言
Release: 2018-05-26 14:40:14
Original
2273 people have browsed it

This time I will show you how to deal with the slow speed caused by too many resources loaded for the first time when lazily loading vue-router. Note What are the matters ? Below are actual cases, let’s take a look at them.

Single-page applications like vue, if there is no application lazy loading, the files packaged with webpack will be abnormally large, resulting in too much content to be loaded when entering the homepage, and the time will be too long, and an error will occur. Ah, first of all, a long white screen is not conducive to the user experience even if loading is done. Using lazy loading can divide the page and load the page when needed, which can effectively share the loading pressure on the homepage and reduce the loading time of the homepage. .

To put it simply: enter the home page without loading too many resources at once, which will cause it to take too long! ! !

Lazy loading method:

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
//采用了懒加载
export default new Router({
 routes: [
  {
    path:'/',
    component:resolve => require(['@/components/index'],resolve)
  }
 ]
})
Copy after login

Non-lazy loading method:

import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
Vue.use(Router)
export default new Router({
 routes: [
  {
    path:'/',
    component:index
  }
 ]
})
Copy after login

ps : Let’s take a look at vue-routerroutinglazy loading

When writing a single-page application with vue.js, the packaged JavaScript will appear The package is very large and affects page loading. We can use lazy loading of routes to optimize this problem. When we use a certain route, we will load the corresponding components. This will be more efficient. The implementation code is as follows:

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
 routes: [
  {
   path: '/',
   component: resolve => require(['components/Hello.vue'], resolve)
  },
  {
    path: '/about',
    component: resolve => require(['components/About.vue'], 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:

Using React Router v4 from scratch

How to use JS to dynamically add HTML tags

The above is the detailed content of How to deal with the slow speed caused by too many resources being loaded for the first time during lazy loading of vue-router. 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