Skip to content

Multiple Alpinejs instances on one page? #3016

Answered by hirasso
hirasso asked this question in 1. Help
Discussion options

You must be logged in to vote

I have found the best approach to be this:

  1. Install Alpine.js from npm and bundle it with your js file (documentation). Don't assign it to window:
import Alpine from "alpinejs";
// Assign a custom prefix:
Alpine.prefix("xyz-");
// Don't assign Alpine to the window (keep it private):
// window.Alpine = Alpine;
Alpine.start();
  1. Use a different prefix for each of your packages
  2. Keep in mind that the custom prefix will disable the @ shorthand for x-on:
<!-- will not work because of `@click` -->
<div xyz-data="{ show: false }" :class="{'is-shown': show}">
    <span xyz-show="show">👀</span>
    <button @click="show = true">Show!</button>
</div>
<!-- will work: -->
<div xyz-data="{ show: false }" 

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@alexmacarthur
Comment options

@hirasso
Comment options

@ekwoka
Comment options

@hirasso
Comment options

@ekwoka
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by hirasso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants