欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中的異步組件函數(shù)實(shí)現(xiàn)代碼

 更新時(shí)間:2018年07月20日 08:32:42   作者:crp205  
這篇文章主要介紹了Vue中的異步組件函數(shù)實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

具體代碼如下所示:

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

上面的代碼是很常見(jiàn)的router代碼分割,只在代碼路由為live才會(huì)去加載live.vue

但這樣在live.vue獲取的過(guò)程將是一片空白,什么也沒(méi)有,體驗(yàn)不好, 利用vue提供的異步組建可以解決

新建一個(gè) loadable.vue

<template>
  <index></index>
</template>
<script>
  import LoadingComponent from './load.vue' // LoadingComponents是 live.vue為獲取前展示的內(nèi)容
  export default {
    components: {
      index: () => ({
        component: import('@/view/live/live.vue'),
        // 異步組件加載時(shí)使用的組件
        loading: LoadingComponent,
        // 展示加載時(shí)組件的延時(shí)時(shí)間。默認(rèn)值是 200 (毫秒)
        delay: 200,
        // 如果提供了超時(shí)時(shí)間且組件加載也超時(shí)了,
        // 則使用加載失敗時(shí)使用的組件。默認(rèn)值是:`Infinity`
        timeout: 3000
      })
    }
  }
</script>

然后修改router.js

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

這樣在獲取到live.vue之前就會(huì)有自定義的loading展示了

但是路由很多, 我們不可能每個(gè)路由都寫(xiě)一個(gè) loadable.vue, 所以編寫(xiě)一個(gè)函數(shù)來(lái)解決

新建一個(gè) loadable.js

import LoadingComponent from './load.vue'
export default (asyncComponent) => {
  const Com= () => ({
    // 這里vue官網(wǎng)可以知道具體有哪些參數(shù)可以設(shè)置
    // 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'))
  }
 ]
})

這樣一個(gè)極簡(jiǎn)的vue異步函數(shù)就完成了

總結(jié)

以上所述是小編給大家介紹的Vue中的異步組件函數(shù)實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論