Let's say I have two Alpine components. How can component two send a message to component one? I would like to do this, but this won't work.
<div x-data="one()" id="one"></div>
<div x-data="two()"></div>
<script>
function one() {
return {
somethingHappened: function(thing) {
console.log("Something happened:", thing)
}
}
}
function two() {
return {
init: function() {
document.getElementById("one").somethingHappened("Wow!")
}
}
}
</script>
Use $dispatch to raise an event in one component, and listen for it using x-on in the other component.
See https://alpinejs.dev/magics/dispatch#dispatching-to-components