#astro client side routing
7 messages · Page 1 of 1 (latest)
Hi @noble ginkgo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro does not use any special component for links like Nuxt's <NuxtLink> component. Instead, you can use standard HTML <a> tags for links in Astro. However, you can create a custom link component in Astro if you want to add specific functionalities like active class highlighting.
Here's an example of how you can create a custom link component in Astro:
---
const { to } = Astro.props
---
<a href={to}><slot /></a>
In this example, to is a prop that specifies the link destination, and <slot /> is where the link text or content will go.
As for adding an active class to the link, you would need to implement this functionality yourself, possibly using JavaScript to add a specific class to the link when its href matches the current page URL.
For more information on migrating from Nuxt to Astro, you can refer to the official Astro migration guide (https://docs.astro.build/en/guides/migrate-to-astro/from-nuxtjs).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@noble ginkgo kapa.ai is still learning and improving, please let us know how it did by reacting below
@knotty temple ok how do i now route to the page becase the href is replaced by the {to} so how do i say where to go when its clicked and also how do i get the current page the client is in to crate the function for addin the active tag
Let me check the knowledge base for you. One moment! :books:
In Astro, you can use the Astro.url.pathname to get the current page's path. Here's an example of how you can use it:
---
import Layout from '../layouts/Layout.astro';
const pagePathname = Astro.url.pathname
---
<Layout title="Home Page" pathname={pagePathname}>
<p>Astro</p>
</Layout>
In this example, pagePathname will hold the current page's path. You can use this to compare with the href of your links and add an active class if they match.
As for the to prop in the custom link component, you would pass it when you use the component, like so:
<CustomLink to="/about">About</CustomLink>
In this example, /about is passed as the to prop to the CustomLink component, and "About" is the link text.
Here's a more complete example of a custom link component with active class functionality:
---
import { Astro } from 'astro/dist/internal';
const { to } = Astro.props;
const isActive = Astro.url.pathname === to;
---
<a class={isActive ? 'active' : ''} href={to}><slot /></a>
In this example, the active class is added to the link if its href matches the current page URL. You can then style the .active class in your CSS to highlight the active link.
Please note that this is a basic example and might not cover all cases. You might need to adjust it according to your needs.