#Astro Component in Javascript

3 messages · Page 1 of 1 (latest)

undone rose
#

Is it possible for me to use the astro component in javascript. I want to use that html in multiple places so want to have consistency.

pine trellisBOT
#

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

#

Yes, you can use Astro components in JavaScript. Astro supports importing and using .html files as components. This is particularly useful if you want to reuse code from an existing site built without a framework, or if you want to ensure that your component has no dynamic features.

Here's an example of how you can import an Astro component in a JavaScript file:

import MyComponent from "../components/MyComponent.astro"

You can then use this component in your Astro pages, layouts, and other components just like Astro components. All your components can live together in /src/components, or can be organized in any way you like.

<MyComponent />

Please note that HTML components must contain only valid HTML, and therefore lack key Astro component features such as support for frontmatter, server-side imports, or dynamic expressions. Any &lt;script&gt; tags are left unbundled, treated as if they had is:inline. They can only reference assets that are in the public/ folder.

For more information, you can refer to the Astro Components (https://docs.astro.build/en/core-concepts/astro-components) section in the Astro documentation.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: