typescript - Vue 3:v-model 助手类型
问题描述
在使用Vue 3 中提供的帮助使 v-model 助手工作后:v-model 助手不是反应式的,它现在可以使用:
import { computed, SetupContext, WritableComputedRef } from 'vue';
export const vModel = <T>(props: Record<string, T>, context: SetupContext, binding = 'modelValue'): WritableComputedRef<T> =>
computed({
get: () => props[binding],
set: (value) => {
context.emit(`update:${binding}`, value);
},
});
但打字不正确。我得到错误:
Argument of type 'Readonly<LooseRequired<Readonly<{ modelValue?: unknown; label?: unknown; } & { modelValue: string; label: string; } & {}> & { [x: string & `on${string}`]: undefined; }>>' is not assignable to parameter of type 'Record<string, string>'.
'string & `on${string}`' and 'string' index signatures are incompatible.
Type 'undefined' is not assignable to type 'string'.
我在函数内尝试了几种类型props
,但它们似乎都不正确。我在 vue 文档中找不到输入的好方法。
解决方案
推荐阅读
- angular - 我想知道哪一个是最常用的模板驱动表单或响应式表单?
- excel - Excel - 基于单元格值的查找数组
- linq - 通过 linQ 查询多对多关系并过滤结果/通过 LINQ 获取动态数据
- content-security-policy - 有没有办法为 CSP 阻止的 iframe 创建后备页面?
- node.js - chrome远程接口ECONNREFUSED 127.0.0.1:9222
- javascript - 如何在触摸设备上单独悬停和点击或移除点击
- c# - 如何设置委托参数的值以用于后续调用而不每次都提供它们?
- javascript - 量角器新手并使用 Visual Studio 代码并在命令行上获取它
- reactjs - 自定义开发者门户以将 api 发布时间放在 devprortal 的概述中
- arrays - 查找具有不等间距范围的表