I use Laravel with Livewire. I wanted to react to an event in js. Once its emitted, an js alert should appear. The problem seems to be in app.js were I have my js function. I receive an error, Livewire is not available. I use Laravel Mix to compile it, and it compiles all fine.
Error: Uncaught TypeError: Livewire.on is not a function
app.js:
import Livewire from '../../vendor/livewire/livewire/dist/livewire';
Livewire.on('testEvent', function () {
alert("test");
});
It looks like an import problem. I've removed the package and tried it again and I also thought its a timing problem, so I executed on document load but still the same problem. I've then decided to push it directly to the stack through a components blade file, and here it works all fine.
@push('scripts')
<script>
Livewire.on('testEvent', function () {
alert("test");
});
</script>
@endpush
Pushing it to the stack should not be required as I've seen other code snippets without it and the error seems to be a simple import problem?