0

I've created an Alpine store like the example:

import Alpine from 'alpinejs'
 
Alpine.store('darkMode', {
    on: false,
 
    toggle() {
        this.on = ! this.on
    }
})
 
Alpine.start()

Is there a way I can export the store to an external file and import it, as you can do with plugins etc.?

Laurel
  • 5,965
  • 14
  • 31
  • 57
panthro
  • 22,779
  • 66
  • 183
  • 324

1 Answers1

0

Yes, you can simply create a separate store.js file with the store definition:

import Alpine from 'alpinejs'
 
Alpine.store('darkMode', {
    on: false,
 
    toggle() {
        this.on = ! this.on
    }
})

And from the main.js (or equivalent) import the store.js file:

import Alpine from 'alpinejs'

import './store.js'
 
Alpine.start()

(I used Vite for testing.)

Dauros
  • 9,294
  • 2
  • 20
  • 28