typescript - 无法创建可能为 null 类型的 Vue ref
问题描述
在 Vue v3 中,我想创建一个类型可能为 null 的 ref。在我的应用程序中,它表示从 null 开始直到完成加载的数据。
作为一个人为的例子:
const firstRef: Ref<{ key: number } | null> = ref(null);
// This should give an error because firstRef.value might be null, but it only errors at runtime
const sum: number = firstRef.value.key + 1;
每当我将鼠标悬停firstRef
在 VSCode 中的类型上时,它总是将类型显示为Ref<{ key: number }>
没有| null
. 难道我做错了什么?或者由于某种原因不可能创建一个可能为空的引用?
解决方案
原来这个问题与 Vue 无关。这个问题没有出现"strict": true
在我compilerOptions
的 in 中tsconfig.json
。
推荐阅读
- r - 在 lidR 中转换 CRS LiDAR 点云
- amazon-web-services - 如何使用 Ansible 停止 AWS RDS 实例?
- json - 无法使用 jsonpath 从 k8s 机密中的地图获取值
- java - 使用 whereGreaterThanOrEqualTo 在字符串中间查找子字符串
- tensorflow - Windows 10 中带有 GPU Quadro M1200 的 TensorFlow
- css - 使用 Toastr,我怎样才能创建一个与默认值完全不同的 toast(我的选择是什么?)
- sonos - 即使服务器不在同一个互联网上也使用 Sonos API - Sonos SOCO & WayScript
- c# - Asp.net Core,在 Razor Page 中设置多个选中的值
- python - 如何在 tkinter 中加粗选定的文本?
- r - 按组对变量求和