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?

王林
王林Original
2023-07-21 20:12:242530browse

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.

  1. Create lazy loading routes
    When using Vue Router to create routes, we can use Vue's asynchronous component features to implement lazy loading. For example, we have a page component named "Home". You can define lazy loading routing as follows:
const Home = () => import('./views/Home.vue')

const router = new VueRouter({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    }
  ]
})
  1. Configuring webpack
    When using lazy loading routing, you need to use Webpack's code splitting function packages asynchronous components into a single file. In the configuration file, the following configuration is required:
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.

  1. Configuring webpack
    First, in the webpack configuration file, you need to use magic comment to specify the components to be preloaded. For example:
const Home = () => import(/* webpackPreload: true */ './views/Home.vue')
  1. Configure routing
    When creating a route, you can use the 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!

Statement:
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