2

自从切换到composition-api并返回一个渲染函数(在我的例子中,使用带有 TypeScript 的 JSX构建以允许在模板中输入支持),我已经失去了vue-devtools检查计算属性的能力。这是因为它们不再由组件直接公开(道具仍然可用)。

如何在模板中获得良好的 TypeScript 支持,同时保留 vue-devtools 检查计算属性的能力?

这是一个使用.vue对 vue-devtools 友好但在模板中不支持 TypeScript 的 composition-api 文件的示例:

SomeComponent.vue

<template>
  <div>
    <ul>
      <li>{{ computedProperty }}</li>
    </ul>
  </div>
</template>

<script lang="ts">
  export default defineComponent({
    name: "SomeComponent",
    props: { 
      // ...
    },
    setup(props, ctx) {
      const computedProperty = computed(() => {
        // return ...some calc
      })

      return {
        computedProperty
      }
    }
  })
</script>

这是一个使用.tsx模板中具有适当 TypeScript 支持的文件的示例,但 vue-devtools 不能再computedProperty直接检查:

SomeComponent.tsx

export default defineComponent({
    name: "SomeComponent",
    props: { 
      // ...
    },
    setup(props, ctx) {
      const computedProperty = computed(() => {
        // return ...some calc
      })

      return () => (
        <div>
          <ul>
            <li>{ computedProperty.value }</li>
          </ul>
        </div>
      )
    }
  })

我怎样才能两全其美?

4

1 回答 1

0
export default defineComponent({
    name: "SomeComponent",
    props: { 
      // ...
    },
    setup(props, ctx) {
      const computedProperty = computed(() => {
        // return ...some calc
      })

      return { computedProperty }
    },
    render() {
      return (
        <div>
          <ul>
            <li>{ this.computedProperty.value }</li>
          </ul>
        </div>
      )
    }
  })
于 2021-06-08T06:19:39.667 回答