-
Describe your problem 🧐Hey there, I'm in a bit of a pickle and could use some help. So, I'm coming from BarbaJS and want to use Swup for my future clients. I've been digging through support tickets and saw that AlpineJS is the way to go with Astro - it's supposed to make handling JavaScript lifecycles a breeze. Never used it before, but I'm giving it a shot. I whipped up a quick demo on my site to test things out. It's just a simple animation where the title on the about page spins when you hover over it. Here's the weird part: it works fine when I load the about page directly, but if I start from the home page and navigate to about, the JavaScript shits the bed. I'm getting an "Alpine expression error" saying "test is not defined" (test is what I named the thing). I'm using Astro with Motion One for animations, and I'm trying to make this work with AlpineJS. Any ideas on how to get this working smoothly?
What is your question? 🙋♀️How can I get this AlpineJS hover animation to work properly when navigating between pages with Swup? I'm aiming for something neat and optimized that just works, you know? If you could throw some example code my way on how to set this up correctly with AlpineJS, that'd be awesome. I'm new to this whole AlpineJS thing, so any tips would be much appreciated. URL to minimal reproduction 🔗Checked all these? 📚
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The simplest way of solving this would be by registering the component in the same file as the Alpine init code. Since Astro loads scripts asynchronously, it's hard to guarantee that the component is defined before the dom element is encounted by Alpine. import Alpine from 'alpinejs'
import test from './alpine-components/test.js'
Alpine.data('test', test)
Alpine.start() |
Beta Was this translation helpful? Give feedback.
The simplest way of solving this would be by registering the component in the same file as the Alpine init code. Since Astro loads scripts asynchronously, it's hard to guarantee that the component is defined before the dom element is encounted by Alpine.