interface Demo {
name: string[]
}
const demo = reactive<Demo>({
name: []
})
const names = ['张三']
//类型“ComputedRef<string[]>”缺少类型“string[]”的以下属性: length, pop, push, concat 及其他 35 项。
这里该如何定义name的类型?
demo.name = computed(() => {
return names
})
reactive结合computed如何定义类型?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
吃不了席 2024-08-30 17:03关注以下回复参考:皆我百晓生、券券喵儿等免费微信小程序作答:
在Vue 3中,
computed函数返回一个ComputedRef类型的对象,而不是直接返回数组。因此,你需要将demo.name的类型更改为ComputedRef<string[]>。同时,你需要从vue包中导入computed和reactive函数。以下是修改后的代码:import { computed, reactive } from 'vue' interface Demo { name: ComputedRef<string[]> } const demo = reactive<Demo>({ name: computed(() => { return names }) }) const names = ['张三']这样,你就可以正确地定义
name的类型并使用computed函数了。解决 无用评论 打赏 举报