因此,在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,它将按预期工作。)
我已经读过this和this并尝试使用this.funcA(y)
来代替,其中它引发了另一个错误
Property 'funcA' does not exist on type 'Vue'.
我想知道使用TypeScript执行此操作的正确方法是什么。
答案 0 :(得分:2)
您可以直接在类上声明funcA
和funcB
方法,如docs所述
vue-class-component:
methods
可以直接声明为类成员方法。
您的组件可以修改为以下内容:
import { Component, Vue } from 'vue-property-decorator';
@Component({})
export default class SomeClass extends Vue {
funcA(x) {
// more code
}
funcB(y) {
this.funcA(y)
}
}