【问题标题】:Are there types for Vue `install` function? Should property `directive` exist on type `Vue`?Vue `install` 函数有类型吗? `Vue` 类型是否应该存在属性`directive`?
【发布时间】:2025-12-12 20:05:01
【问题描述】:

我正在为我的 vue 指令打字,但我似乎找不到我应该为 Vue 安装功能使用什么类型。

使用install(Vue: any): void 似乎有点奇怪。

我尝试过导入Vue,然后使用install(Vue: Vue),但是会抛出一个错误,Vue 上没有.directive,这也很奇怪。

我应该如何输入安装函数? Vue 类型是否应该在这里工作?我在这里遗漏了什么,或者Vue 遗漏了什么?

import { DirectiveOptions } from "vue"


const directive: DirectiveOptions = {
  bind(elem, bind, vn) {
    console.log('bound')
  }
};

export default {
  install(Vue: any): void { // what types should be used here?
    Vue.directive("do-stuff", directive);
  }
};

【问题讨论】:

    标签: typescript vue.js vuejs2 typescript-typings vue-directives


    【解决方案1】:

    你想要的类型是VueConstructor。以下是如何编写 Typescript Vue 插件

    import { VueConstructor, PluginObject, DirectiveOptions } from "vue"
    
    const directive: DirectiveOptions = {
      bind(elem, bind, vn) {
        console.log('bound')
      }
    }
    
    const plugin: PluginObject<any> = {
      install (Vue: VueConstructor): void {
        Vue.directive("do-stuff", directive)
      }
    }
    
    export default plugin
    

    https://github.com/vuejs/vue/blob/dev/types/vue.d.ts#L80

    【讨论】: