Jinuss's blog Jinuss's blog
首页
  • 源码合集

    • Leaflet源码分析
    • Openlayers源码合集
    • vue3源码
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

东流

Web、WebGIS技术博客
首页
  • 源码合集

    • Leaflet源码分析
    • Openlayers源码合集
    • vue3源码
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • React

    • 废弃Create-React-App
  • Vue

  • JavaScript文章

  • 学习笔记

  • openlayers

  • threejs

  • MapboxGL

  • 工具

  • 源码合集

  • 前端
  • Vue
东流
2024-09-12
目录

import.meta.glob的介绍与使用

# 概述

import.meta 元属性将特定上下文的元数据暴露给 JavaScript 模块。它包含了这个模块的信息,例如这个模块的 URL。在vue3项目中,用的比较多的是通过import.meta.env来获取环境变量。而本文将要介绍的import.meta.glob和import.meta.env都是vite提供的功能。

import.meta.glob 允许动态地加载多个模块而不需要显式地列出每个模块地路径。如果需要根据目录结构自动导入文件时,import.meta.glob会非常有帮助。

# 为什么要用import.meta.glob

这里以实际工作中的一个功能需求为例。

需求:通过后端返回的type去加载不同的表单form,

文件目录结构如下:

  • 不用import.meta.glob
<script setup>
import { ref, watch, computed } from "vue";
import { storeToRefs } from "pinia";
import { SYMBOL_TYPES } from "../../../const/const.map";
import Point from "./forms/Point.vue";
import Polygon from "./forms/Polygon.vue";
import Line from "./forms/Line.vue";
import Model from "./forms/Model.vue";
import Billboard from "./forms/Billboard.vue";
import Billboard_HLJB from "./forms/Billboard_HLJB.vue";
import Label from "./forms/Label.vue";
import LabelExt from "./forms/LabelExt.vue";
import { useMapMarkStore } from "../../../store/useMapMarkStore";

const mapMarkerStore = useMapMarkStore();

const { currentSymbol } = storeToRefs(mapMarkerStore);

const symbolInfo = ref({ ...currentSymbol.value });

watch(currentSymbol, (newValue) => {
  symbolInfo.value = newValue;
});

const currentComponent = computed(() => {
  const { type } = symbolInfo.value;
  switch (type) {
    case SYMBOL_TYPES.Polyline:
      return Line;
    case SYMBOL_TYPES.Point:
      return Point;
    case SYMBOL_TYPES.Polygon:
      return Polygon;
    case SYMBOL_TYPES.Model:
      return Model;
    case SYMBOL_TYPES.Label:
      return Label;
    case SYMBOL_TYPES.Billboard:
      return Billboard;
    case SYMBOL_TYPES.Billboard_HLJB:
      return Billboard_HLJB;
    default:
      return null;
  }
});
</script>
<template>
  <div class="styleManageContent">
    <component :is="currentComponent" />
  </div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

上面的代码主要是通过watch和computed去监听type,再通过switch去匹配type返回不同的form组件。

  • 使用import.meta.glob
<script setup>
import { ref, watch, onMounted } from "vue";
import { storeToRefs } from "pinia";
import { SYMBOL_TYPES } from "../../../const/const.map";
import LabelExt from "./forms/LabelExt.vue";
import { useMapMarkStore } from "../../../store/useMapMarkStore";

const mapMarkerStore = useMapMarkStore();

const { currentSymbol } = storeToRefs(mapMarkerStore);

const symbolInfo = ref({ ...currentSymbol.value });

watch(currentSymbol, (newValue) => {
  symbolInfo.value = newValue;
  getCurrentComponent();
});

const currentComponent = ref({});

const getCurrentComponent = async () => {
  const { type } = symbolInfo.value;
  const modules = import.meta.glob(`./forms/*.vue`);
  for (let path in modules) {
    const componentName = path.split("/").pop().split(".").shift();
    if (componentName == type) {
      modules[path]().then((module) => {
        currentComponent.value = module.default;
      });
      break;
    }
  }
};

onMounted(() => {
  getCurrentComponent();
});
</script>
<template>
  <div class="styleManageContent">
    <component :is="currentComponent" />
    <LabelExt v-if="symbolInfo.type != SYMBOL_TYPES.Label" />
  </div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

使用import.meta.glob,代码就简化了很多,不用导入所有的表单组件,也不用额外去定义常量SYMBOL_TYPES。而且由于import.meta.glob是动态导入,懒加载的方式会提高性能。

  • 这里需要注意的一点就是type值需要和组件名对应上,或者有一个对应关系。

# import.meta.glob介绍

# 基本用法

import.meta.glob接受一个字符串参数,且该参数不能是一个变量,因为import.meta.glob的参数在构建时会被静态分析,用于生成构建时的文件列表,使用变量会导致这些路径在构建时无法明确具体的路径,导致Vite无法解析和生成响应的模块导入代码。

# 关键点

  • 路径匹配:支持路径通配符

  • 返回值:返回值是一个对象,键是匹配的文件路径,值是动态导入函数

  • 动态导入:返回的值可以动态导入,是一个异步操作

# 用途

  • 动态加载路由组件、插件、视图等

  • 实现懒加载以优化应用性能

编辑 (opens new window)
上次更新: 2025/04/09, 10:15:29
最近更新
01
ECMAScript新特性汇总
07-14
02
GeoJSON
05-08
03
Circle
04-15
更多文章>
Theme by Vdoing | Copyright © 2024-2025 东流 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式