#reading props

1 messages · Page 1 of 1 (latest)

tawny epochBOT
#

Hi @proven heath. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

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:


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

proven heath
#

how else i can do it?

proven heath
#

not working at all... any help appreciated... @radiant basin

radiant basin
proven heath
#

but this happens if i use it on <Picture. i use on <img successfully.

radiant basin
#

Ah, that i missed: img works, picture doesnt?

proven heath
proven heath
proven heath
radiant basin
#

we are not talking about import { Picture } from 'astro:assets'; but RafidMuhymin/astro-imagetools, right?

proven heath
radiant basin
#

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