在 Vue2 中,异步组件和路由懒加载处理使用 import 就可以很轻松实现。但是在Vue 3.x 中异步组件的使用与 Vue 2.x 完全不同了。本文就详细讲讲vue3中异步组件和路由懒加载的实现。
Vue3 异步组件/路由
现在,在 Vue 3 中,由于函数组件被定义为纯函数,异步组件定义需要通过将其包装在一个新的 defineAsyncComponent helper 中来显式定义。
const asyncPage = () => import('./views/home.vue')
<template> <div> <h1>Async Components</h1> <p>异步组件测试</p> <child /> </div> </template> <script> import { defineAsyncComponent } from 'vue' const child = defineAsyncComponent(() => import('@/components/async-component-child.vue')) export default { name: 'async-components', components:{ 'child': child } }; </script>
const asyncPageWithOptions = { component: () => import('./views/home.vue'), delay: 200, timeout: 3000, error: ErrorComponent, loading: LoadingComponent }
所以,下面的异步组件声明:
const asyncPage = () => import('./views/home.vue')
等价于:
const asyncPageWithOptions = { component: () => import('./views/home.vue') }
const asyncPageWithOptions = defineAsyncComponent({ loader: () => import('./views/home.vue'), delay: 200, timeout: 3000, error: ErrorComponent, loading: LoadingComponent })
// 2.x version const oldAsyncComponent = (resolve, reject) => { /* ... */ }
// 3.x version const asyncComponent = defineAsyncComponent( () => new Promise((resolve, reject) => { /* ... */ }) )
Vue 3.x的异步组件加载函数将不再接收resolve和reject,而且必须始终返回Promise。也就是说,工厂函数接收 resolve 回调的方式定义异步组件在 Vue 3.x 不能使用了。
// 在 Vue 3.x 中不适用 export default { components: { asyncPage: resolve => require(['@/components/list.vue'], resolve) }, }
提示: 如果是用vite工具来构建项目,在本地开发使用import做路由懒加载,可以正常加载,但是会报警告;打包到生产环境会报错,页面不会正常展示,可以使用以下两种方法来实现。
// router/index.js import { defineAsyncComponent } from 'vue' const _import = (path) => defineAsyncComponent(() => import(`../views/${path}.vue`)); const routes = [ { path: '/async-component', name: 'asyncComponent', component: _import('home'), } ];
// 1.上面的方法相当于一次性加载了 views 目录下的所有.vue文件,返回一个对象 const modules = import.meta.glob('../views/*/*.vue'); const modules ={ "../views/about/index.vue": () => import("./src/views/about/index.vue") } // 2.动态导入的时候直接,引用 const router = createRouter({ history: createWebHistory(), routes: [ // ... { path: 'xxxx', name: 'xxxxx', // 原来的方式,这个在开发中可行,但是生产中不行 // component: () => import(`../views${menu.file}`), // 改成下面这样 component: modules[`../views${filename}`] } // ... ], })
<template> <div> <h1>Async Components</h1> <p>异步组件测试</p> <child></child> </div> </template> <script> import { defineAsyncComponent } from 'vue' const child = defineAsyncComponent(() => import('@/components/async-component-child.vue')) export default { name: 'async-components', components:{ 'child': child } }; </script>
简单来说,写在路由配置文件中的异步加载就是路由懒加载的用法,而写在组件内部的异步加载就是异步组件用法。