Vue 3 模板函数引用是什么类型?
VUE 3允许使用一个函数来分配REF,
const target = ref<Element>()
const functionRef = (ref: Element) => {
target.value = ref
}
<template>
<div :ref="functionRef" />
</template>
但是,Volar和Typescript都会抱怨:ref =“ FunctionRef”
绑定具有类型不匹配。
类型'(ref:element)=&gt; void'无法分配到type'字符串|参考| ((ref:element | componentPublicInstance&lt; {},{},{},{},{},{},{},{},{},{},false,false,componentOptionsbase&lt; and oshy,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何,任何任何,{}&gt;&gt; |未定义'。
Runtime-dom.d.t.ts(1479,3):预期类型来自属性“ ref”,该属性在类型的“ elementattrs”
上在此处声明
是什么函数ref的类型?
Vue 3 allows a function to be used to assign a ref
const target = ref<Element>()
const functionRef = (ref: Element) => {
target.value = ref
}
<template>
<div :ref="functionRef" />
</template>
However, Volar and TypeScript will both complain that the :ref="functionRef"
binding has a type mismatch.
Type '(ref: Element) => void' is not assignable to type 'string | Ref | ((ref: Element | ComponentPublicInstance<{}, {}, {}, {}, {}, {}, {}, {}, false, ComponentOptionsBase<any, any, any, any, any, any, any, any, any, {}>> | null) => void) | undefined'.
runtime-dom.d.ts(1479, 3): The expected type comes from property 'ref' which is declared here on type 'ElementAttrs'
What is the type of a function ref?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Runtime-dom.d.ts
链接包含以下定义。该定义可以在源在这里。
重要的部分是
ref?
的第三个联合类型,这意味着您可以为
ref Ref
参数定义可重复使用的类型,如下所示。您现在可以使用ARG的新类型更新功能参考
The
runtime-dom.d.ts
link contains the following definition.This definition can be found in the source here.
The important part is third union type for
ref?
This means you can define a reusable type for the
ref
argument as follows.You can now update your function ref with the new type for the arg