我正在使用 Vue 的组合 API(在 Vue.js 3 中)并主要在setup(). 虽然通过直接访问我自己的 props ,但我无法以类型安全的方式公开此处定义的函数。setup(props)methods
以下内容有效,但我需要任意转换,因为没有向 TypeScript 公开的方法接口。
<!-- MyComponent.vue -->
<script lang='ts'>
// ...
export default defineComponent({
setup() {
// ...
return {
publicFunction: async (): Promise<void> => { /* ... */ };
}
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
<!-- AppComponent.vue -->
<template>
<MyComponent ref="my"/>
</template>
<script lang='ts'>
export default defineComponent({
async setup() {
const my = ref();
async func() {
await (my.value as any).publicFunction(); // <-- gross!
}
return { …Run Code Online (Sandbox Code Playgroud)