Strongly typing props of vue components using composition api and typescript typing system

前端 未结 2 2112
臣服心动
臣服心动 2021-02-18 14:37

I am using vue composition api with typescript.

How can I strongly type the component props using typescript typing system?

2条回答
  •  野性不改
    2021-02-18 15:09

    As explained in the official docs you can type props in two ways:

    Define arops via argument annotation

    import { defineComponent } from 'vue'
    
    export default defineComponent((props: { foo: string }) => {
      props.foo
    })
    

    Or like this

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

提交回复
热议问题