[英]how to declare methods in child vue component?
child.vue:
<script lang="ts">
import { Vue } from 'vue-property-decorator'
@Component
export default class Child extends Vue {
public func(): void {
//
}
}
</script>
父.vue:
<template>
<child ref="child" />
</template>
<script lang="ts">
import { Vue } from 'vue-property-decorator'
import Child from './child.vue'
export default Parent extends Vue {
public create (): void {
(this.$refs.child as typeof Child).func()
}
}
TSLint 錯誤:
Property 'func' does not exist on type 'Component<DefaultData<never>, DefaultMethods<never>, DefaultComputed, DefaultProps>'.
如何讓'typeof Child'被檢測為類Child? 我知道創建一個額外的接口然后導入它可以解決這個問題,但是還有其他方法嗎?
第一:您忘記了父組件上的@Component
裝飾器。 它不會正確鍵入。
第二個(可選):您可以使用@Ref
裝飾器來保留ref
的類型化引用。
第三:您不需要typeof
關鍵字。
@Ref('child')
readonly childComponent: Child
created () {
// Correctly typed
this.childComponent.func()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.