BinHong
BinHong

Reputation: 165

Vue.js 2.x TypeScript - Calling a method in another method in the same component

So in the <script> part of a vue file I have something like the following:

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

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

export default class SomeClass extends Vue {}

With TypeScript, the above code throws an error of

Cannot find name 'funcA'.

(It works as intended if defined as JavaScript instead.)

I've read this and this and tried using this.funcA(y) instead in which it throws an different error of

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

I'm wondering what is the proper way to do this with TypeScript.

Upvotes: 0

Views: 1764

Answers (1)

Ash
Ash

Reputation: 11472

You can declare funcA and funcB methods directly on the class as noted in the docs for vue-class-component:

  1. methods can be declared directly as class member methods.

Your component can be modified to the following:

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

@Component({})

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

Upvotes: 2

Related Questions