Home > Article > Web Front-end > How to use Vue Router to implement lazy loading and preloading of routes?
How to use Vue Router to implement lazy loading and preloading of routes?
Vue Router is the official routing manager of Vue.js. It can help us implement the front-end routing function, which is very important for single page applications (SPA). In actual projects, as pages increase and functions become richer, lazy loading and preloading of routes are commonly used optimization methods. This article will introduce how to use Vue Router to achieve these two functions.
1. Lazy Loading of Routing
Under normal circumstances, we need to package all page components into a JavaScript file, so that the entire page will be loaded when it is first loaded. The application code is downloaded to the browser. But when the application becomes more and more complex, the size of this JavaScript file will become larger and larger, resulting in a long initial loading time. To solve this problem, you can use lazy loading of routes.
const Home = () => import('./views/Home.vue') const router = new VueRouter({ routes: [ { path: '/', name: 'Home', component: Home } ] })
module.exports = { // ... output: { // ... chunkFilename: 'js/[name].[chunkhash].js' }, // ... optimization: { splitChunks: { chunks: 'async', minSize: 30000, maxSize: 0, minChunks: 1, maxAsyncRequests: 5, maxInitialRequests: 3, automaticNameDelimiter: '~', cacheGroups: { vendors: { test: /[\/]node_modules[\/]/, priority: -10 }, default: { minChunks: 2, priority: -20, reuseExistingChunk: true } } } } }
After this configuration, webpack will automatically package the asynchronous component into a separate file and add the appropriate hash value as the file name. Implement asynchronous loading.
2. Preloading of routing (Preloading)
In lazy loading of routing, page components will only be loaded when accessed, and each page will only be loaded once. However, in some scenarios, we may need to load the code of some page components during application initialization to improve the response speed during subsequent visits. This requires using the preloading function of routing.
magic comment
to specify the components to be preloaded. For example: const Home = () => import(/* webpackPreload: true */ './views/Home.vue')
webpackChunkName
option to name the preloaded page component for easy differentiation. For example: const router = new VueRouter({ routes: [ { path: '/', name: 'Home', component: () => import(/* webpackChunkName: "home" */ './views/Home.vue') }, // ... ] })
In this way, when the application is initialized, Vue Router will automatically preload the Home component and load the page component code in advance.
Summary
By using Vue Router’s lazy loading and preloading functions, we can effectively optimize front-end application performance.
In actual projects, depending on the number and complexity of page components, you can flexibly choose lazy loading or preloading to improve the application's loading speed and user experience.
I hope this article can help you understand how to use Vue Router to implement lazy loading and preloading of routes. I wish you all good luck in your studies!
The above is the detailed content of How to use Vue Router to implement lazy loading and preloading of routes?. For more information, please follow other related articles on the PHP Chinese website!