Last active
November 10, 2022 13:35
-
-
Save fvanzeijl/ad48a352b6f05adb422539b72ad6e013 to your computer and use it in GitHub Desktop.
Register npm modules globaly in Alpinejs via alpine.store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import modules | |
import Swiper from 'swiper' | |
// Import alpine | |
import Alpine from 'alpinejs' | |
window.Alpine = Alpine | |
// Register module in alpines store | |
Alpine.store('Swiper', Swiper) | |
// Start alpine | |
Alpine.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div x-data="mySliderComponent"> | |
<div class="swiper"> | |
... | |
</div> | |
</div> | |
<script> | |
document.addEventListener('alpine:init', () => { | |
Alpine.data('mySliderComponent', () => ({ | |
slider: null, | |
init() { | |
if(this.$refs.slider) { | |
this.slider = new this.$store.Swiper(this.$refs.slider, {}) | |
} | |
} | |
}) | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment