How to execute function in app.js from a livewire component with alpine.js? Hello,
I am trying to access functions in app.js from a livewire component with alpine.js "x-init".
For testing purposes I have this function in my app.js:
const sayHello = () => {
alert('hello');
}
And in the livewire component:
I get this error: alpine.js:1834 Uncaught ReferenceError: sayHello is not defined
Sry, I am new in Javascript, how can I define the function before it is invoked?
Thank you!
Solved: Sry, just realized that the code is minified, so it can't find the function...
I am trying to rebuild the date picker example with livewire and alpinejs and getting a ReferenceError for the Pikaday object too.
I am also assuming, the problem has to do with the minification of app.js.
Since, it is an example from the livewire author, I really wonder, how that is meant to work.
As far as I can remember, my solution was to attach the function to the window object. It will not be minified and you can access it from you PHP/html file
Yes, that's it. I figured it out just today :-)
In the case of the pikaday example, you would edit your standard laravel app.js file like this:
import pikaday from "pikaday";
window.Pikaday = pikaday;
Then it should work. I did not try further the pikaday example, but it works with my own scripts.
Please sign in or create an account to participate in this conversation.