繁体   English   中英

如何在基于 Vue class 的 typescript 组件上声明全局混合和过滤器?

[英]How to declare global mixins and filters on Vue class based typescript component?

我有一个 Vue 2 的插件文件,我像这样使用它:

global-helpers.ts

import clone from 'lodash/clone'

class GlobalHelpers {
    install(Vue) {
        Vue.filter('clone', clone)
        Vue.mixin({
            methods: {
                clone,
            }
        })
    }
}

export default new GlobalHelpers()

globals.ts

import Vue from "vue"
import GlobalHelpers from './global-helpers.ts'
Vue.use(GlobalHelpers)

Products.vue

import {Component, Vue} from 'vue-property-decorator'
import {State, Action} from 'vuex-class'

export default class Products extends Vue {
    cloneItem(item) {
            this.item = this.clone(item)
    }
}

这会导致TS2339: Property 'clone' does not exist on type 'Products'.

我尝试了以下方法:

declare module 'vue/types/vue' {
    interface VueConstructor {
        clone: (item) => any
    }
}

declare module 'vue/types/options' {
    interface ComponentOptions<V extends Vue> {
        clone: (item) => any
    }
}

但这似乎不起作用......你能帮我解释一下我应该如何在 typescript 上声明这些属性吗?

好的,如果我把它放在 global-helpers ts 里面...

declare module 'vue/types/vue' {
    interface Vue {
        cloneDeep: (item) => any
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM