Vue和TypeScript如何在render中获取data属性?

时间:2018-08-20 02:20:42

标签: typescript vue.js

当我使用TypeScript时,我试图在渲染中使用它。 但它无法运行:

  

TS2339:类型上不存在属性“选项”   'ComponentOptions,DefaultMethods,   默认计算,PropsDefinition

import Vue from 'vue'

declare module 'vue/types/vue' {
    interface Vue {
        onSwtich: Function
    }
}

export default Vue.extend({
    name: 'footerTabs',
    data() {
        return {
            options: {
                on: {
                    click: el => this.onSwtich(el)
                }
            }
        }
    },
    render(h) {
        return h('swiper', {
            class: 'page-footer-tabs',
            attrs: {
                options: this.options  // TS2339: Property 'options' does not exist on type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Rec...'.
            }
        })
    },
    methods: {
        onSwtich(el): void {
            this.$emit('on-switch', el)
        }
    }
})

我尝试向

添加选项
  

界面Vue {     选项:对象   }

它仍然无法正常工作

1 个答案:

答案 0 :(得分:1)

declare module 'vue/types/options' {
    interface ComponentOptions<V extends Vue> {
        options?: any
    }
}

事实证明,它需要像这样声明。