Subscribe on changes!

How to use directives in Vue 3 Web Component library?

avatar
Feb 8th 2022

What problem does this feature solve?

This feature would somehow allow vue components which have no app instance to install directives.

We are building a Vue 3 Web Component library. We don't create an app, instead we just export our components individually. We would like to be able to install directives, but it requires an app as input.

What I've done as a work around is:

import { createApp, Directive } from 'vue';
import VWave from 'v-wave';
const app = createApp({});
app.use(VWave);

export const Wave: Directive = {
  ...app._context.directives.wave
}

What does the proposed API look like?

Vue.directive('name': string, directive: Directive);

avatar
Feb 9th 2022

I'd say in this specific example, the package should not force you to register the directive globally and offer local import as well.

I'll close this as we have a more general issue tracking something similar in #4635