Home > Web Front-end > JS Tutorial > body text

A brief analysis of Vue's asynchronous component functions

不言
Release: 2018-07-19 17:17:15
Original
1484 people have browsed it

This article brings you a brief analysis of Vue’s asynchronous component functions. It has corresponding code examples. Friends in need can refer to them.

export default new Router({
  routes: [
    {
      path: '/live',
      name: 'live',
      component: () => import('@/view/live/live.vue')
    }
  ]
})
Copy after login

The above code is a very common router code split. Live.vue will only be loaded when the code is routed to live
But in this way, the process of obtaining live.vue will be blank, and there will be nothing. No, the experience is not good, it can be solved by using the asynchronous assembly provided by vue

新建一个 loadable.vue

<template>
    <index></index>
</template>

<script>
    import LoadingComponent from './load.vue' // LoadingComponents是 live.vue为获取前展示的内容
    export default {
        components: {
            index: () => ({
                component: import('@/view/live/live.vue'),
                // 异步组件加载时使用的组件
                loading: LoadingComponent,
                // 展示加载时组件的延时时间。默认值是 200 (毫秒)
                delay: 200,
                // 如果提供了超时时间且组件加载也超时了,
                // 则使用加载失败时使用的组件。默认值是:`Infinity`
                timeout: 3000
            })
        }
    }
</script>

然后修改router.js

export default new Router({
  routes: [
    {
      path: '/live',
      name: 'live',
      component: import('loadable.vue')
    }
  ]
})
Copy after login

In this way, there will be a customized loading display before getting live.vue
But there are many routes, and we can't have every Each route writes a loadable.vue, so write a function to solve

新建一个 loadable.js

import LoadingComponent from './load.vue'

export default (asyncComponent) => {
    const Com= () => ({
        // 这里vue官网可以知道具体有哪些参数可以设置
        // https://cn.vuejs.org/v2/guide/components-dynamic-async.html#%E5%A4%84%E7%90%86%E5%8A%A0%E8%BD%BD%E7%8A%B6%E6%80%81
        component: asyncComponent(),
        loading: LoadingComponent
    })
    return {
        render (h) {
          return h(Com, {})
        }
    }
}

然后修改一下router.js

import loadable from 'loadable.js'
export default new Router({
  routes: [
    {
      path: '/live',
      name: 'live',
      component: loadable (() => import('@/view/live/live.vue'))
    }
  ]
})
Copy after login

Such a minimalist vue asynchronous function is completed

Related recommendations:

How VSCode introduces Vue components and Js modules

The above is the detailed content of A brief analysis of Vue's asynchronous component functions. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!