import { type App, defineAsyncComponent } from 'vue'
export function install (app: App) {
const importComponents = import.meta.glob('../components/**/*.vue');
for (const path in importComponents) {
const componentName = path.substring(
path.lastIndexOf('/') + 1,
path.lastIndexOf('.')
)
app.component(componentName, defineAsyncComponent(importComponents[path] as any))
}
}
main.ts 引用
import { install } from './components'
import './assets/main.css'
import App from './App.vue'
import { createApp } from 'vue'
const app = createApp(App)
install(app);
app.mount('#app')