Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Eloïse's avatar

Alpine V3 : Unable to access x-data component values since I upgraded

Hello, Since I updated Alpine I'm unable to access x-data object, I changed like mentionned in the documentation but i get this error : caught ReferenceError: test is not defined

<div x-data="test">
        <span @click="prout" x-text="">coucou</span>
    </div>

    @endsection

@push('scripts')
    <script src="{{asset('js/home/home.js')}}"></script>
    <script>

        document.addEventListener('alpine:init', () => {
            Alpine.data("test", () => ({
                text: "salut",
                prout: () => {
                    this.text = "oups!"
                }

            }))
        })
0 likes
9 replies
LaryAI's avatar
Level 58

In Alpine.js v3, the Alpine.data() method has been replaced with Alpine.data() function. Therefore, the code should be updated as follows:

<div x-data="test">
    <span @click="prout" x-text="text">coucou</span>
</div>

@push('scripts')
    <script src="{{asset('js/home/home.js')}}"></script>
    <script>
        document.addEventListener('alpine:init', () => {
            Alpine.data('test', () => ({
                text: 'salut',
                prout() {
                    this.text = 'oups!';
                }
            }));
        });
    </script>
@endpush
newbie360's avatar

@Eloïse something like this ?

    <div x-data="{ text: 'apple', prout(fruit) { this.text = fruit} }" class="space-x-4">
        <button @click="prout('banana')">banana</button>
        <button @click="prout('orange')">orange</button>

        <span x-text="text"></span>
    </div>
Eloïse's avatar

Thanks for your reply this works but what if I want to have x-data outside in a script ? If I made that I got the previous error message

this should work but it doesn't : caught ReferenceError: test is not defined

<div x-data="test" class="space-x-4">
        <button @click="prout('banana')">banana</button>
        <button @click="prout('orange')">orange</button>

        <span x-text="text"></span>
    </div>

    @endsection

@push('scripts')
    <script src="{{asset('js/home/home.js')}}"></script>
    <script>
        
        document.addEventListener('alpine:init', () => {
            Alpine.data('test', () => ({
                text: 'apple',
                prout(fruit) {
                    this.text = fruit
                }
            }));
        });
newbie360's avatar

@Eloïse

<div x-data="test" class="space-x-4">
    <button @click="prout('banana')">banana</button>
    <button @click="prout('orange')">orange</button>

    <span x-text="text"></span>
</div>

@push('scripts')
    <script>
        document.addEventListener('alpine:init', () => {
            Alpine.data('test', () => ({
                text: 'apple',

                prout(fruit) {
                    this.text = fruit
                }
            }))
        })
    </script>
@endpush
Eloïse's avatar

My bad ! I've forget to add window.Alpine = Alpine in app.js

Eloïse's avatar

@cwhite I mix this in another js inline file and finally use it in the view so it's not used on every pages.

Please or to participate in this conversation.