Dynamically Import Images
Local images must be imported into .astro
files in order to display them. There will be times where you will want or need to dynamically import the image paths of your images instead of explicitly importing each individual image.
In this recipe, you will learn how to dynamically import your images using Vite’s import.meta.glob
function. You will build a card component that displays the name, age, and photo of a person.
Recipe
Section titled Recipe- Create a new
assets
folder under thesrc
directory and add your images inside that new folder.
目录src/
目录assets/
- avatar-1.jpg
- avatar-2.png
- avatar-3.jpeg
-
Create a new Astro component for your card and import the
<Image />
component.src/components/MyCustomCardComponent.astro ---import { Image } from 'astro:assets';--- -
Specify the
props
that your component will receive in order to display the necessary information on each card. You can optionally define their types, if you are using TypeScript in your project.src/components/MyCustomCardComponent.astro ---import { Image } from 'astro:assets';interface Props {imagePath: string;altText: string;name: string;age: number;}const { imagePath, altText, name, age } = Astro.props;--- -
Create a new
images
variable and use theimport.meta.glob
function which returns an object of all of the image paths inside theassets
folder.src/components/MyCustomCardComponent.astro ---import { Image } from 'astro:assets';interface Props {imagePath: string;altText: string;name: string;age: number;}const { imagePath, altText, name, age } = Astro.props;const images = import.meta.glob('/src/assets/*.{jpeg,jpg,png,gif}')--- -
Use the props to create the markup for your card component.
src/components/MyCustomCardComponent.astro ---import { Image } from 'astro:assets';interface Props {imagePath: string;altText: string;name: string;age: number;}const { imagePath, altText, name, age } = Astro.props;// Type: Record<string, () => Promise<{default: ImageMetadata}>>const images = import.meta.glob('/src/assets/*.{jpeg,jpg,png,gif}')---<div class="card"><h2>{name}</h2><p>Age: {age}</p><Image src={} alt={altText} /></div> -
Inside the
src
attribute, pass in theimages
object and use bracket notation for the image path. Then make sure to invoke the glob function.src/components/MyCustomCardComponent.astro ---import { Image } from 'astro:assets';interface Props {imagePath: string;altText: string;name: string;age: number;}const { imagePath, altText, name, age } = Astro.props;const images = import.meta.glob('/src/assets/*.{jpeg,jpg,png,gif}')---<div class="card"><h2>{name}</h2><p>Age: {age}</p><Image src={images[imagePath]()} alt={altText} /></div>
-
Import and use the card component inside an Astro page, passing in the values for the
props
.src/pages/index.astro ---import MyCustomCardComponent from '../components/MyCustomCardComponent.astro';---<MyCustomCardComponentimagePath="/src/assets/avatar-1.jpg"altText="A headshot of Priya against a brick wall background."name="Priya"age={25}/>
更多操作指南
-
状态共享
学习如何使用 Nano Stores 在框架组件之间共享状态
-
添加 RSS 摘要
通过向你的 Astro 网站添加 RSS 摘要让用户来订阅你的内容。
-
安装一个 Vite 或 Rollup 插件
了解如何通过向项目添加 Rollup 插件来导入 YAML 数据。
-
构建自定义图像组件
了解如何使用 getImage 函数构建支持媒体查询的自定义图像组件
-
使用 API 路由构建表单
了解如何使用 JavaScript 发送表单到 API 路由
-
在 Astro 页面中构建 HTML 表单
了解如何在 Astro 页面中构建 HTML 表单并在你的 frontmatter 中处理提交请求
-
在 Astro 中使用 Bun
了解如何在 Astro 站点中使用 Bun。
-
调用服务器端点
了解如何在 Astro 中调用服务器端点
-
校验验证码
了解如何创建一个 API 路由并从客户端进行获取。
-
用 Docker 来构建你的 Astro 网站
了解如何使用 Docker 来构建你的 Astro 网站。
-
Dynamically Import Images
Learn how to dynamically import images using Vite's import.meta.glob function
-
为链接添加图标
了解如何安装 rehype 插件并为你 Markdown 文件中的链接添加图标
-
添加 i18n 功能
通过动态路由和内容集合来让你的 Astro 站点支持国际化。
-
添加最后修改时间
编写一个在你的 Markdown 和 MDX 文件里添加最后修改时间的 remark 插件。
-
添加阅读时间
编写一个在你的 Markdown 和 MDX 文件里添加阅读所需时间的 remark 插件。
-
在 Astro 组件中共享状态
了解如何通过 Nano Stores 在 Astro 组件中共享状态
-
使用流式处理来提升页面性能
了解如何使用流式处理来提升页面性能
-
使用 Tailwind Typography 美化渲染后的 Markdown
了解如何使用 @tailwind/typography 美化你渲染后的 Markdown