Hello,
I'm a junior developer learning about Laravel and Alpine and I ran into a problem with linking an alpine object to my view.
context:
I've imported alpine into my project via the terminal with:
./vendor/bin/sail npm install alpinejs
Then I included the following code in my app.js:
import Alpine from 'alpinejs';
window.Alpine = Alpine;
Alpine.start();
In my js folder is also the file home.js, which I included in my home.blade.php using @vite() and registered in the vite.config.js.
I've tested and the home.js is propperly linked and alpine can be logged in the terminal.
problem:
When I make an alpine object like this:
document.addEventListener("alpine:init", () => {
Alpine.data('testing', () => ({
message: 'Hello from Alpine!'
}));
});
And try to use it like this:
<div x-data="testing">
<p x-text="message"></p>
</div>
I get this error: Alpine Expression Error: testing is not defined
Does anyone know the correct way to create, link and use alpine objects in laravel?