使用语法糖 /* @async */ 包裹 vue 组件引用,会自动转换为 defineAsyncComponent 异步组件。
- Install the plugin:
npm install vite-plugin-async-component -D- Add the plugin to your Vite config:
// vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import asyncComponentPlugin from 'vite-plugin-async-component';
export default defineConfig({
plugins: [vue(), asyncComponentPlugin()]
});
- Use the @async comment blocks in your code:
/* @async */
import ComponentA from './index-a.vue';
import ComponentB from './index-b.vue';
/* @endasync */
Testing Run tests using Vitest:
npm test