首頁  >  文章  >  web前端  >  vue3 vite非同步組件及路由懶載入怎麼應用

vue3 vite非同步組件及路由懶載入怎麼應用

PHPz
PHPz轉載
2023-05-18 16:52:143194瀏覽

一、前言

1-1.三點變化:

  • #非同步元件宣告方法的改變:Vue 3.x 新增一個輔助函數defineAsyncComponent,用來顯示宣告非同步元件

  • 非同步元件進階宣告方法中的component 選項更名為loader

  • loader綁定的元件載入函式不再接收resolve和reject參數,而且必須回傳一個Promise

1-2.引入輔助函數defineAsyncComponent的原因:

現在,在Vue 3 中,由於函數元件被定義為純函數,非同步元件定義需要透過將其包裝在一個新的defineAsyncComponent helper 中來明確定義。

二、Vue 2.x與Vue 3.x定義比較

2-1.非同步元件/路由定義比較

  • 2-1 -1.在Vue 2.x 中,宣告一個非同步元件只需這樣:

const asyncPage = () => import('./views/home.vue')
  • 2-1-2.在Vue 3.x 中,非同步元件的導入需要使用輔助函數defineAsyncComponent來進行明確宣告。如下:

<template>
  <div>
    <h2>Async Components</h2>
    <p>异步组件测试</p>
    <child />
  </div>
</template>
<script>
import { defineAsyncComponent } from &#39;vue&#39;
const child = defineAsyncComponent(() => import(&#39;@/components/async-component-child.vue&#39;))
export default {
  name: &#39;async-components&#39;,
  components:{
    &#39;child&#39;: child
  }
};
</script>

2-2.宣告方式比較

  • #2-2-1.Vue 2.x中非同步元件的宣告有更進階的聲明方式。如下:

const asyncPageWithOptions  = {
  component: () => import(&#39;./views/home.vue&#39;),
  delay: 200,
  timeout: 3000,
  error: ErrorComponent,
  loading: LoadingComponent
}

所以,下面的非同步元件宣告:

const asyncPage = () => import(&#39;./views/home.vue&#39;)

等價於:

const asyncPageWithOptions  = {
  component: () => import(&#39;./views/home.vue&#39;)
}
  • 2-2 -2.Vue 3.x中也可以這樣宣告非同步元件。只是其中的component需要改為loader。如下:

const asyncPageWithOptions  = defineAsyncComponent({
  loader: () => import(&#39;./views/home.vue&#39;),
  delay: 200,
  timeout: 3000,
  error: ErrorComponent,
  loading: LoadingComponent
})

2-3.非同步元件載入函數傳回比較

  • 2-3-1.在Vue 2.x中接收resolve和reject:

// 2.x version
const oldAsyncComponent = (resolve, reject) => {
  /* ... */
}
  • 2-3-2.在Vue 3.x中總是回傳Promise:

#
// 3.x version
const asyncComponent = defineAsyncComponent(
  () => new Promise((resolve, reject) => {
      /* ... */
  })
)

Vue 3.x的非同步元件載入函數將不再接收resolve和reject,而且必須始終傳回Promise。換句話說,在Vue 3.x中,不再支援透過工廠函數接收resolve回呼來定義非同步元件。

// 在 Vue 3.x 中不适用
export default {
  components: {
    asyncPage: resolve => require([&#39;@/components/list.vue&#39;], resolve)
  },
}

三、Vue3實踐

提示: 如果是用vite工具來構建項目,在本地開發使用import做路由懶加載,可以正常加載,但是會報警告;打包到生產環境會報錯,頁面不會正常展示,可以使用以下兩種方法來實現。

3-1.路由懶載入實作

  • 3-1-1.defineAsyncComponent方法

// router/index.js
import { defineAsyncComponent } from &#39;vue&#39;
const _import = (path) => defineAsyncComponent(() => import(`../views/${path}.vue`));
const routes = [
  {
    path: &#39;/async-component&#39;,
    name: &#39;asyncComponent&#39;,
    component: _import(&#39;home&#39;),
  }
];
  • #3-1-2.import.meta.glob方法

// 1.上面的方法相当于一次性加载了 views 目录下的所有.vue文件,返回一个对象
const modules = import.meta.glob(&#39;../views/*/*.vue&#39;);
const modules ={
    "../views/about/index.vue": () => import("./src/views/about/index.vue")
}
// 2.动态导入的时候直接,引用
const router = createRouter({
  history: createWebHistory(),
  routes: [
    // ...
    {
      path: &#39;xxxx&#39;,
      name: &#39;xxxxx&#39;,
      // 原来的方式,这个在开发中可行,但是生产中不行
      // component: () => import(`../views${menu.file}`),
      // 改成下面这样
      component: modules[`../views${filename}`]
    }
    // ...          
  ],
})

3-2.非同步元件實作

<template>
  <div>
    <h2>Async Components</h2>
    <p>异步组件测试</p>
    <child></child>
  </div>
</template>
<script>
import { defineAsyncComponent } from &#39;vue&#39;
const child = defineAsyncComponent(() => import(&#39;@/components/async-component-child.vue&#39;))
export default {
  name: &#39;async-components&#39;,
  components:{
    &#39;child&#39;: child
  }
};
</script>

以上是vue3 vite非同步組件及路由懶載入怎麼應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除