Alpine.plugin()
Register a plugin to extend Alpine functionality.
What is Alpine.plugin()?
Alpine.plugin() is the entry point for all Alpine extensions. Plugins receive the Alpine instance and can add custom directives, magic properties, or even alter Alpine's behaviour. Official plugins like Persist, Mask, and Focus are registered this way.
When to Use
Use Alpine.plugin() when you need to package and share reusable Alpine functionality, or when using official extensions.
Creating a Custom Plugin
Code
Alpine.plugin((Alpine) => {
Alpine.magic('log', () => msg => console.log(msg))
})
// Then use $log('Hello') in any componentInstalling Official Plugins
Code
import persist from '@alpinejs/persist'
import Alpine from 'alpinejs'
Alpine.plugin(persist)
Alpine.start()💡 Tip
Always call Alpine.plugin() before Alpine.start(). Plugins often need to register their functionality during Alpine's initialisation.
Helpful? Share this page!↑ Back to top