Can we create Mixins in Stimulus?


#1

Hey :slight_smile:

I’m using stimulus on a side project and I wanted to create a Mixin (aka methods you can inject in a controller) to avoid duplication of my code in different controllers.

What’s the “right” way in stimulus to do that.

Basically I’m looking for that concept: https://vuejs.org/v2/guide/mixins.html


#2

You can extend controllers as described here to inherit the targets/methods. That’s been pretty useful for me.

You can also have multiple stimulus controllers per DOM element, essentially letting you mix and match groups of behaviors on the fly. I find it nicest to keep stimulus methods concerned with just responding to events on targets and light data/dom manipulation and tuck more complex logic away in other modules.