首页 > 其他分享 >TypeScript 与组合式 API

TypeScript 与组合式 API

时间:2024-04-10 17:47:14浏览次数:32  
标签:组合式 TypeScript const string PropType API props 类型 defineProps

看吧:https://cn.vuejs.org/guide/typescript/composition-api.html
为组件的 props 标注类型

<script setup lang="ts">
const props = defineProps({
  foo: { type: String, required: true },
  bar: Number
})
props.foo // string
props.bar // number | undefined
</script>

这被称之为“运行时声明”,因为传递给 defineProps() 的参数会作为运行时的 props 选项使用。
然而,通过泛型参数来定义 props 的类型通常更直接:

<script setup lang="ts">
const props = defineProps<{
  foo: string
  bar?: number
}>()
</script>

这被称之为“基于类型的声明”。我们也可以将 props 的类型移入一个单独的接口中:

<script setup lang="ts">
interface Props {
  foo: string
  bar?: number
}
const props = defineProps<Props>()
</script>

组件传值示例:<MyComponent :foo="fooStr" :bar="barStr" @callback="search()"></MyComponent>
若要传入一个对象,则可以稍微改变一下:

<script setup lang="ts">
interface Props {
  foo: string
  bar?: number
}
const props = defineProps<{Info:Props}>();
</script>

组件传值示例:<MyComponent :Info="propsObj" @callback="search()"></MyComponent>
propsObj就是interface Props 类型的实例。

复杂的 prop 类型
通过基于类型的声明,一个 prop 可以像使用其他任何类型一样使用一个复杂类型:

<script setup lang="ts">
interface Book {
  title: string
  author: string
  year: number
}

const props = defineProps<{
  book: Book
}>()
</script>

对于运行时声明,我们可以使用 PropType 工具类型:

import type { PropType } from 'vue'

const props = defineProps({
  book: Object as PropType<Book>
})

其工作方式与直接指定 props 选项基本相同:

import { defineComponent } from 'vue'
import type { PropType } from 'vue'

export default defineComponent({
  props: {
    book: Object as PropType<Book>
  }
})

 

标签:组合式,TypeScript,const,string,PropType,API,props,类型,defineProps
From: https://www.cnblogs.com/xsj1989/p/18126522

相关文章

  • gemini1.5 API调用
    https://ai.google.dev/pricing?hl=zh-cn查询可用的modelhttps://generativelanguage.googleapis.com/v1beta/models?key=xxx使用postman调用https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=xxx https://ai......
  • 最新阿里云服务器esc centos7 系统 安装yapi全流程 亲测
    一、环境准备安装yapi前,需部署node与mongodb我这里用到的版本=》node:v14.15.1mongodb:v4.2.23yapi:v1.8.0注意操作之前需要阿里云服务器安全组开放9090端口 这一步省略了1.node安装 1.1下载node,解压  使用wget直接下载。wgethttps://nodejs.org/download/re......
  • 【TensorRT】TensorRT C# API 项目更新 (1):支持动态Bath输入模型推理(下篇)
    4.接口应用关于该项目的调用方式在上一篇文章中已经进行了详细介绍,具体使用可以参考《最新发布!TensorRTC#API:基于C#与TensorRT部署深度学习模型》,下面结合Yolov8-cls模型详细介绍一下更新的接口使用方法。4.1创建并配置C#项目 首先创建一个简单的C#项目,然后添加项......
  • 20211318 实验一-密码引擎-3-加密API研究
    任务详情:密码引擎API的主要标准和规范包括:微软的CryptoAPIRAS公司的PKCS#11标准中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交博客链接......
  • 实验一-密码引擎-3-加密API研究
    实验详情:密码引擎API的主要标准和规范包括:1微软的CryptoAPI2RAS公司的PKCS#11标准3中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交博客......
  • 【大模型应用开发-FastAPI框架】(五)FastAPI 如何通过Poetry运行FastAPI应用程序
    一、概述FastAPI是一个现代、快速(高性能)的Web框架,用于构建API。Poetry是一个Python的依赖管理和打包工具,可以帮助我们更有效地管理项目的依赖和环境。在本文中,我们将介绍如何使用Poetry来运行FastAPI应用程序。二、安装FastAPI和Poetry在开始之前,我们需要先安装FastAPI和P......
  • Rust 标准库 API 文件和文件夹操作 File,读取/创建/修改/追加/删除/重命名文件等
    File::create使用File的关联函数(类似Java中的静态方法)create,创建文件,如果存在,则覆盖。usestd::fs::{File,Metadata};fnmain()->std::io::Result<()>{letfile:File=File::create("foo.txt")?;letmetadata:Metadata=file.metadata()?;println!......
  • 密码引擎-3-加密API研究
    任务内容研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代码,提交博客链接和代码链接。内容:0查找各种标准的原始文档,研究学习(至少包含CryptoAPI,PKCS#11,GMT0016-2012,GMT0018-2012)(5分)1总结这些API在编程中的使用方式(5分)2列出这些API包含的函数......
  • 加密API研究
    实验一-密码引擎-3-加密API研究查找各种标准的原始文档,研究学习(至少包含CryptoAPI,PKCS#11,GMT0016-2012,GMT0018-2012)(5分)微软的CryptoAPIhttps://learn.microsoft.com/zh-cn/windows/win32/seccrypto/cryptoapi-system-architectureRAS公司的PKCS#11标准https://docs......
  • 实验一-密码引擎-3-加密API研究
    ##一、任务概览密码引擎API的主要标准和规范包括:1微软的CryptoAPI2RAS公司的PKCS#11标准3中国商用密码标准:GMT0016-2012智能密码钥匙密码应用接口规范,GMT0018-2012密码设备应用接口规范等研究以上API接口,总结他们的异同,并以龙脉GM3000Key为例,写出调用不同接口的代......