【问题标题】:Vue.extend() + TypeScript = methods must specify event: MouseEvent, how can I avoid it?Vue.extend() + TypeScript = 方法必须指定事件:MouseEvent,我该如何避免呢?
【发布时间】:2020-11-16 17:07:00
【问题描述】:

请看这个最小的例子

import Vue from "vue";

export default Vue.extend({
  props: {
    text: String,
  },
  methods: {
    click() {
      console.log(this.text); // Property 'text' does not exist on type 'Vue'.Vetur(2339)
    },
  },
});

但是,如果我指定事件类型,一切都会正常

import Vue from "vue";

export default Vue.extend({
  props: {
    text: String,
  },
  methods: {
    click(event: MouseEvent) {
      console.log(this.text); // Everything is fine right now!
    },
  },
});

但这会导致另一个问题,我不需要为我的组件使用event 参数。

虽然我可以写click(_: MouseEvent),但是会导致代码不清晰。

如何避免打字事件?

【问题讨论】:

    标签: typescript vue.js vuejs2


    【解决方案1】:

    请像这样定义道具并重试

    props: {
       text: {
          type: String,
          default: ''
       }
    }
    

    【讨论】:

    • 没有解决问题,我认为是vetur的问题,虽然vetur在大喊大叫,但是build没有输出任何类型错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多