由于切换到composition-api并返回一个render function (在我的例子中,使用JSX with TypeScript构造以允许在模板中输入支持),我已经失去了让vue-devtools检查computed properties的能力。这是因为它们不再由组件直接公开(道具仍然可用)。
如何在模板中获得良好的TypeScript支持,同时保留vue-devtools检查计算属性的能力?
下面是一个使用.vue文件的例子,它对vue-devtools很友好,但在模板中没有TypeScript支持:
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>
)
}
})我怎样才能两全其美呢?
发布于 2021-06-08 14:19:40
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>
)
}
})https://stackoverflow.com/questions/61911008
复制相似问题