#reading props
1 messages · Page 1 of 1 (latest)
In Astro, when you import an image, it doesn't return a simple string with the path of the image anymore. Instead, it returns an object that matches the following signature:
interface ImageMetadata {
src: string;
width: number;
height: number;
format: string;
}
So, when you import an image like this:
import image from 'src/assets/images/image.jpg';
The image variable is an object, not a string. If you want to get the URL of the image, you can access the src property of the image object:
const imageUrl = image.src;
This will give you the URL of the image as a string.
If you want to pass this URL to a component and read it outside of the component, you can do it like this:
<hero
image={{
src: image.src,
alt: 'Welcome msg!',
}}
/>
Then, in your hero component, you can access the src value from the image prop:
const { image } = Astro.props;
const imageUrl = image.src;
This will give you the URL of the image as a string outside of the component.
Sources:
- Astro Documentation: Images (https://docs.astro.build/en/guides/images#update-existing-img-tags)
- Astro Documentation: Dynamically import images (https://docs.astro.build/en/recipes/dynamically-importing-images#recipe)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@proven heath kapa.ai is still learning and improving, please let us know how it did by reacting below
how else i can do it?
not working at all... any help appreciated... @radiant basin
What error message do you get?
hi Martin, this is so weird, i get Input file is missing or props.src.replace is not a function.
but this happens if i use it on <Picture. i use on <img successfully.
Ah, that i missed: img works, picture doesnt?
😦 yess...
import Image from '~/components/common/Image.astro';
import { Picture } from "astro-imagetools/components";
it happens when i use on <Picture
we are not talking about import { Picture } from 'astro:assets'; but RafidMuhymin/astro-imagetools, right?
oh sorry, so thats not official? He is using <Picture /> Sorry then, confused..
no astro-imagetools seems to be a community solution going back to the days before Erika reworked the Image and Picture support. Haven't looked deeper but it might be that it is has problems with astro 4 / vite 5. npm reported 4 high severity vulnerabilities when i installed it. Didn't work for me either.
official packages (beside astro itself) typical use the @astrojs prefix