相关疑难解决方法(0)

Vue 3 组合 API 定义方法的类型安全方式是什么

我正在使用 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)

typescript vuejs3 vue-composition-api

3
推荐指数
1
解决办法
2183
查看次数

标签 统计

typescript ×1

vue-composition-api ×1

vuejs3 ×1