Summary of 3 ways to lazy load vue-router

Summary of 3 ways to lazy load vue-router

Not using lazy loading

import Vue from 'vue';
import Router from 'vue-router';
import HelloWorld from '@components/HelloWorld';
Vue.use(Router);
export default new Router({
routes:[
	{path:'./',
	name:'HelloWorld',
	component:HelloWorld
	}
]
})

vue asynchronous component

component:resolve=>{reuqire(['The routing address to be loaded']),resolve)

import Vue from 'vue';
import Router from 'vue-router';
const HelloWorld=resolve=>{require(["@/components/HelloWorld"],resolve}
Vue.use(Router);
export default new Router({
routes:[
	{path:'./',
	name:'HelloWorld',
	component:HelloWorld
	}
]
})

ES6 import()

import Vue from 'vue';
import Router from 'vue-router';
import HelloWorld=()=>import('@/components/HelloWorld');
Vue.use('Router')
export default new Router({
	routes:[{
	{path:'./',
	name:'HelloWorld',
	component:HelloWorld
	}
	}]
})

webpack's require.ensure()

require.ensure can load resources on demand, including js, css, etc. It will package the required files separately and will not package them together with the main file.

The first parameter is an array, indicating the modules required by the second parameter, which will be loaded in advance.

The second is the callback function. In this callback function, the required file will be packaged into a separate chunk and will not be packaged together with the main file. In this way, two chunks are generated. Only the main file is loaded during the first load.

The third parameter is the error callback.

The fourth parameter is the file name of the separately packaged chunk

import Vue from 'vue';
import Router from 'vue-router';
const HelloWorld=resolve=>{
		require.ensure(['@/components/HelloWorld'],()=>{
			resolve(require('@/components/HelloWorld'))
		})
	}
Vue.use('Router')
export default new Router({
	routes:[{
	{path:'./',
	name:'HelloWorld',
	component:HelloWorld
	}
	}]
})

Summarize

This concludes this article about the three ways of lazy loading of vue-router. For more relevant content on lazy loading of vue-router, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue router passes parameters and solves the problem of parameter loss when refreshing the page
  • How to use Vue-router routing
  • Two implementation codes of Vue-router programmatic navigation
  • Vue-router routing lazy loading and 3 ways to implement it
  • Detailed explanation of the difference between hash mode and history mode in Vue-router
  • vue-router defines meta information meta operation
  • Vue router installation and usage analysis
  • Initial practice of vue3.0+vue-router+element-plus
  • How to handle the loss of parameters when refreshing the page when passing parameters to vue router

<<:  Solution to changing the data storage location of the database in MySQL 5.7

>>:  Detailed explanation of Nginx access restriction configuration

Recommend

Analysis of the cause of docker error Exited (1) 4 minutes ago

Docker error 1. Check the cause docker logs nexus...

MySQL Server 8.0.13.0 Installation Tutorial with Pictures and Text

Install 8.0.13 based on MySQL 6.1.3. MySQL 8.0.13...

Initial summary of the beginner's website building tutorial

After writing these six articles, I started to fee...

mysql5.7.18.zip Installation-free version configuration tutorial (windows)

This is the installation tutorial of mysql5.7.18....

Detailed explanation of Tomcat configuration and optimization solutions

Service.xml The Server.xml configuration file is ...

This article takes you into the world of js data types and data structures

Table of contents 1. What is dynamic typing? 2. D...

Correct steps to install Nginx in Linux

Preface If you are like me, as a hard-working Jav...

Detailed explanation of various ways to merge javascript objects

Table of contents Various ways to merge objects (...

React Synthetic Events Explained

Table of contents Start by clicking the input box...

A brief discussion on the principle of Vue's two-way event binding v-model

Table of contents explain: Summarize Replenish Un...

How to capture exceptions gracefully in React

Table of contents Preface ErrorBoundary Beyond Er...

What does input type mean and how to limit input

Common methods for limiting input 1. To cancel the...

Advanced techniques for using CSS (used in actual combat)

1. The ul tag has a padding value by default in Mo...

Native js canvas to achieve a simple snake

This article shares the specific code of js canva...