I have the same problem. I also want to use $wire inside Alpine.data(). I tried wrapping the JavaScript part inside @script/@endscript directives. But that doesn't work either. Have you found a solution in the meantime?
Jun 19, 2023
5
Level 1
$wire in Alpine.data()
How to access the Livewire component from the method declared in Alpine.data() (similar to the $wire magic method)
You need to move the method call from
<div x-data>
<button @click="$wire.showModal().then( () => console.log('OK') )"</button>
</div>
to
<div x-data="modals">
<button @click="showModal()"</button>
</div>
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('modals', () => ({
showModal() {
$wire.showModal().then( () => console.log('OK') ) // $wire is not defined
}
}))
})
</script>
- This is an example to understand what is needed
Please or to participate in this conversation.