前端动态导入(import.meta.glob)

import.meta.glob 简介

这是 Vite 提供的一个特殊导入函数,用于批量导入模块文件。它支持使用 glob 模式匹配文件,非常适合需要动态导入多个模块的场景。

参数说明

typescript 复制代码
import.meta.glob(pattern, {
  as?: 'url' | 'raw' | 'string',    // 导入格式
  eager?: boolean,                   // 是否同步导入
  import?: string | string[],        // 指定导入的内容
  query?: Record<string, string | number | boolean> // 查询参数
})

详细使用示例

  1. 基本异步导入
typescript 复制代码
// 导入所有 .vue 组件
const modules = import.meta.glob('./components/*.vue')

// 使用方式
for (const path in modules) {
  modules[path]().then((mod) => {
    console.log(path, mod)
  })
}
  1. 同步导入
typescript 复制代码
// 立即导入所有组件
const modules = import.meta.glob('./components/*.vue', { eager: true })

// 可以直接使用
console.log(modules['/components/Button.vue'].default)
  1. 导入特定内容
typescript 复制代码
// 只导入组件中的 setup 函数
const modules = import.meta.glob('./components/*.vue', {
  import: 'setup',
  eager: true
})

// 导入多个导出内容
const modules = import.meta.glob('./components/*.vue', {
  import: ['setup', 'data'],
  eager: true
})
  1. 不同格式导入
typescript 复制代码
// URL 格式导入
const imageUrls = import.meta.glob('./assets/*.png', {
  as: 'url'
})

// 原始文本导入
const textContents = import.meta.glob('./files/*.txt', {
  as: 'raw'
})

// 字符串导入
const strings = import.meta.glob('./locales/*.json', {
  as: 'string'
})
  1. 复杂匹配模式
typescript 复制代码
// 多模式匹配
const modules = import.meta.glob([
  './components/**/*.vue',  // 所有子目录的 vue 文件
  './layouts/*.vue',        // layouts 目录下的 vue 文件
  '!./components/test/*'    // 排除 test 目录
])

实际应用场景

  1. 自动注册 Vue 组件
typescript 复制代码
// 自动注册全局组件
const components = import.meta.glob('./components/*.vue', { eager: true })

export function registerComponents(app) {
  Object.entries(components).forEach(([path, module]) => {
    const name = path.match(/\.\/components\/(.*)\.vue$/)?.[1]
    if (name) {
      app.component(name, module.default)
    }
  })
}
  1. 动态路由配置
typescript 复制代码
// 自动生成路由配置
const pages = import.meta.glob('./pages/**/*.vue')

const routes = Object.entries(pages).map(([path, component]) => ({
  path: path
    .replace('./pages', '')
    .replace('.vue', '')
    .replace(/\/index$/, '/'),
  component: component
}))
  1. 多语言文件管理
typescript 复制代码
// 导入所有语言文件
const locales = import.meta.glob('./locales/*.json', {
  eager: true,
  as: 'string'
})

// 处理语言文件
const messages = Object.entries(locales).reduce((acc, [path, content]) => {
  const lang = path.match(/\.\/locales\/(.*)\.json$/)?.[1]
  if (lang) {
    acc[lang] = JSON.parse(content)
  }
  return acc
}, {})

使用注意事项

  1. 路径规则:
  • 必须使用字符串字面量
  • 支持相对路径(./)和绝对路径(/)
  • 不支持动态变量拼接
  1. 性能考虑:
typescript 复制代码
// 异步导入(推荐)- 实现代码分割
const modules = import.meta.glob('./modules/*.ts')

// 同步导入 - 所有模块打包到主包
const modules = import.meta.glob('./modules/*.ts', { eager: true })
  1. 通配符说明:
typescript 复制代码
* // 匹配任意字符(不含路径分隔符)
** // 匹配任意字符(包含路径分隔符)
? // 匹配单个字符
[abc] // 匹配方括号中的任意字符
{a,b} // 匹配花括号中的任意模式
  1. 类型支持:
typescript 复制代码
// 类型声明
interface GlobModule {
  [path: string]: () => Promise<any>
}

const modules: GlobModule = import.meta.glob('./modules/*.ts')
相关推荐
晓得迷路了1 小时前
栗子前端技术周刊第 156 期 - jQuery 4.1、TanStack Query v6 路线图...
前端·javascript·node.js
恋猫de小郭1 小时前
站起来,Flutter 要支持真正的 3D Texture 了
android·前端·flutter
Felven12 小时前
C. Make Them Equal
c语言·开发语言·前端
智能收银研究所16 小时前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店
java·前端·数据库
滑动变滚动的蜗牛18 小时前
我发布了自己的个人小程序(诉述洞)
前端·ai编程·全栈
ZYH_060119 小时前
Shell 第一阶段笔记:变量、引号与变量截取
前端·chrome·笔记
YIAN20 小时前
流式 TTS 和普通TTS到底有什么区别?一个文档教会你!
前端·websocket·node.js
掘金安东尼21 小时前
智能体包办工作时,批判性思维怎么办(Addy Osmani 全文转译)
前端·程序员·agent
SameX1 天前
HarmonyOS ArkTS Canvas 画环形进度条 animateTo + @Watch 驱动重绘 120 行实现
前端
欧雷殿1 天前
我的 AI 外包实战:用数字员工做项目的一人公司
前端·ai编程·团队管理