Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
alpine.js data() x-data directive

I'm using alpine.js v3 with some modules for the data

import Foo from './foo';
import Bar from './bar';

window.Alpine = Alpine;

window.Alpine.data('data',  () => ({
    foo: Foo(),
    bar: Bar(),
}));

window.Alpine.start();
<body x-data="data">

Whenever I try to access the data <button @click="data.foo.doSomething()"> I get the error

Alpine Expression Error: data is not defined

Do I have to add it with the x-data directive anyways?


(update)

And how can I access this data from another component, e.g. Get status of the Foo component and call a method of Bar:

window.Alpine.bind('SomeButton', (name) => ({
    'x-init'(){
        const data = window.Alpine.$data;
            
        if(data.foo.status === 'open'){
            data.bar.doSomething();
        }
    }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

With Alpine.data() you define a component. After that you can access directly all of the properties of the component inside the element where you applied it via x-data. Here data is the component, so you don't have to prefix its properties with data.:

<div x-data="data">
  <button @click="foo.doSomething()">Call doSomething()</button>
</div>

Answering your second question: since you have one "global" component that you applied to the topmost element (body), each child component can access the properties of this parent component. So inside SomeButton "bind-object"-type component, we can access them in the component's definition using the this. prefix.

window.Alpine.bind('SomeButton', () => ({
    '@click'() {
        if (this.foo.status === 'open') {
            this.bar.doSomething()
        }
    }
})

Here we attached our logic to the click-event: we call bar.doSomething() if the status is 'open'.

<div x-data="data">
    <button x-bind="SomeButton">Call bar.doSomething() if status is 'open'</button>
</div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!