Alpine: Is there any router for alpinejs?

Created on 26 Mar 2020  路  5Comments  路  Source: alpinejs/alpine

Is there any router for alpinejs?

Most helpful comment

Just to give an example: if you really, really want to do routing (or even some sort of show and hide thing based on the URL) you can do

<div x-data="{page: location.hash}" @hashchange.window="page = location.hash">
<span x-show="page === ''">Index Page</span>
<span x-show="page === '#contact'">Contact Page</span>
</div>
...
<a href="#contact">Contact Us!</a>

No external framework required!

All 5 comments

There is no router, you can read the URL and replace/add to it using DOM APIs directly (window.location, URL object, and history API)

Alpine isn't really meant for building entire single page applications. It's possible, but not what it's meant for.

Thanks for weighing in everyone.

Just to give an example: if you really, really want to do routing (or even some sort of show and hide thing based on the URL) you can do

<div x-data="{page: location.hash}" @hashchange.window="page = location.hash">
<span x-show="page === ''">Index Page</span>
<span x-show="page === '#contact'">Contact Page</span>
</div>
...
<a href="#contact">Contact Us!</a>

No external framework required!

Was this page helpful?
0 / 5 - 0 ratings