Vue 3 模板函数引用是什么类型?

发布于 2025-01-18 07:52:24 字数 795 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云不回头 2025-01-25 07:52:24
import { ComponentPublicInstance } from 'vue';
const functionRef = (ref: ComponentPublicInstance | null | Element) => {
  target.value = ref
}
<div :ref="functionRef" />

import { ComponentPublicInstance } from 'vue';
const functionRef = (ref: ComponentPublicInstance | null | Element) => {
  target.value = ref
}
<div :ref="functionRef" />

望喜 2025-01-25 07:52:24

Runtime-dom.d.ts链接包含以下定义。

type ReservedProps = {
  key?: string | number | symbol
  ref?:
    | string
    | RuntimeCore.Ref
    | ((ref: Element | RuntimeCore.ComponentPublicInstance | null) => void)
  ref_for?: boolean
  ref_key?: string
}

该定义可以在源在这里

重要的部分是ref?的第三个联合类型,

(ref: Element | RuntimeCore.ComponentPublicInstance | null) => void

这意味着您可以为ref Ref参数定义可重复使用的类型,如下所示。

// this may not be necessary depending on where you put this definition
import * as RuntimeCore from '@vue/runtime-core'

type VueRef = (ref: Element | RuntimeCore.ComponentPublicInstance | null) => void

您现在可以使用ARG的新类型更新功能参考

const target = ref<Element>()

const functionRef = (ref: VueRef) => {
  target.value = ref
}

The runtime-dom.d.ts link contains the following definition.

type ReservedProps = {
  key?: string | number | symbol
  ref?:
    | string
    | RuntimeCore.Ref
    | ((ref: Element | RuntimeCore.ComponentPublicInstance | null) => void)
  ref_for?: boolean
  ref_key?: string
}

This definition can be found in the source here.

The important part is third union type for ref?

(ref: Element | RuntimeCore.ComponentPublicInstance | null) => void

This means you can define a reusable type for the ref argument as follows.

// this may not be necessary depending on where you put this definition
import * as RuntimeCore from '@vue/runtime-core'

type VueRef = (ref: Element | RuntimeCore.ComponentPublicInstance | null) => void

You can now update your function ref with the new type for the arg

const target = ref<Element>()

const functionRef = (ref: VueRef) => {
  target.value = ref
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文