Vue.js 2.x TypeScript-在同一组件中的另一个方法中调用一个方法

时间:2019-06-03 01:16:44

标签: typescript vue.js vuejs2

因此,在vue文件的<script>部分中,我有以下内容:

import { Component, Vue } from 'vue-property-decorator';

@Component({
    methods: {
        funcA(x) {
            // more code
        },
        funcB(y) {
            funcA(y)
        },
    },
})

export default class SomeClass extends Vue {}

使用TypeScript,上面的代码将引发错误

Cannot find name 'funcA'.

(如果将其定义为JavaScript,它将按预期工作。)

我已经读过thisthis并尝试使用this.funcA(y)来代替,其中它引发了另一个错误

Property 'funcA' does not exist on type 'Vue'.

我想知道使用TypeScript执行此操作的正确方法是什么。

1 个答案:

答案 0 :(得分:2)

您可以直接在类上声明funcAfuncB方法,如docs所述 vue-class-component

  
      
  1. methods可以直接声明为类成员方法。
  2.   

您的组件可以修改为以下内容:

import { Component, Vue } from 'vue-property-decorator';

@Component({})

export default class SomeClass extends Vue {
  funcA(x) {
    // more code
  }
  funcB(y) {
    this.funcA(y)
  }
}