Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda