K
K
Kotaro Hiba2021-03-31 07:34:02
typescript
Kotaro Hiba, 2021-03-31 07:34:02

How to declare a vue directive?

There is a 'v-click-outside' plugin that adds a custom directive, I'm trying to connect it

declare module 'vue/types/vue' {
  interface Vue {
    vClickOutside: () => void;
  }
}
import vClickOutside from 'v-click-outside';


If you work with plugins, then this method worked with a plugin and a mixin How to make your own plugin for Vue2 if the project is in TypeScript? , they helped me with the answer, then I already found in the dock how to implement TypeScript support in Vue
https://ru.vuejs.org/v2/guide/typescript.html
But I can’t understand how to declare a directive, for the life of me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-03-31
@bingo347

If you need to extend the existing Vue interface with your own elements, then put a .d.ts file in the project root whose name does not match the other files.

import 'vue'; // нужно чтоб слинковаться с базовыми модулем и интерфейсом
declare module 'vue' { // расширяем модуль
  import vClickOutside from 'v-click-outside'; // подключаем типы плагина
  interface Vue { // расширяем интерфейс
    // прописываем нужные расширения
    vClickOutside(): typeof vClickOutside;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question