1

I am using Alpine JS store() to make a global function available to toggle a modal status.

Module:

export default () => ({
    /**
     * @property isOpen - controls the state of the Contact Form Modal
     */
    isOpen: false,

    open() {
        this.isOpen = true
    },

    close() {
        this.isOpen = false
    },
});

Then I use it like:

import modalContactForm from './alpine/modal-contact-form';

// Alpine.store sets data for GLOBAL usage
Alpine.store('modalContactForm', modalContactForm);
Alpine.start();

And with this HTML:

<button x-data @click="$store.modalContactForm.open()"></button>

But I get:

Alpine Expression Error: $store.modalContactForm.open is not a function

And I don't know how to debug this situation.

Interestingly, if I pass the object directly into Alpine.store(), it works.

Alpine.store('modalContactForm', {
        isOpen: false,
    
        open() {
            this.isOpen = true
        },
    
        close() {
            this.isOpen = false
        },
    });
Alpine.start();

1 Answer 1

1

In the external module file export the store as a simple object, not as an anonymous function:

const modalContactForm = {
    /**
     * @property isOpen - controls the state of the Contact Form Modal
     */
    isOpen: false,

    open() {
        this.isOpen = true
    },

    close() {
        this.isOpen = false
    },
}

export default modalContactForm
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.