typescript - 使用组合 API 和 typescript 打字系统对 vue 组件进行强打字

标签 typescript vue.js strong-typing vue-composition-api

我正在使用带有 typescript 的 vue 组合 api。

如何使用 typescript 打字系统对组件 Prop 进行强类型输入?

最佳答案

正如官方 docs 中解释的那样您可以通过两种方式输入 props:

通过参数注释定义 arops

import { defineComponent } from 'vue'

export default defineComponent((props: { foo: string }) => {
  props.foo
})

或者像这样
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    foo: String
  },
  setup(props) {
    props.foo // <- type: string
  }
})

关于typescript - 使用组合 API 和 typescript 打字系统对 vue 组件进行强打字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59966387/

相关文章:

objective-c - Objective-C 中是否存在强类型集合?

c# - 强类型 Linq 过滤方法

actionscript-3 - ActionScript (Vector <T,T>) 中具有多个基本类型的强类型集合?

javascript - Json 数据到 typescript 模型

typescript - 为什么 throw 使函数端点无法访问?

reactjs - 在 node_modules/jest-diff/build/diffLines.d.ts 中构建时出现 React + Typescript 错误

javascript - 如何在 vue.js 中的嵌套 v-for 循环中访问元素的 ref 索引?

typescript - 在 TypeScript 中限制数组的值

vue.js - Vue - v-for 修改值

javascript - 用于本地编辑的 Vuex 克隆对象