Vue3 props的使用详解

编程入门 行业动态 更新时间:2024-10-25 22:37:38

Vue3 props的使用<a href=https://www.elefans.com/category/jswz/34/1770044.html style=详解"/>

Vue3 props的使用详解

Props 声明

1、字符串数组声明props

<script setup lang="ts">
const props = defineProps(["cat"])console.log(props.cat)
</script>

 2.对象实现props

<script setup lang="ts">
const props = defineProps({cat:string
})
</script>//可以在模板中直接使用cat变量
<template>{{ cat }}
</template>

你还可以使用类型标注,这是ts的特性。

<script setup lang="ts">
const props = defineProps<{cat?:string
}>()
</script>//或者使用接口interface animal{cat?:string
}const props = defineProps<animal>()

3、使用camelCase(小驼峰命名法),可以在模板中直接使用(如第一个例子)。看代码

defineProps({getSex: String
})<template>{{getSex}}
</template>

4、动态绑定props

import {reactive} from "vue"const data=reactive({article:{cat:"tom"
}
})//下方传递这个cat<span :animal='data.article.cat'></span>//然后你就可以改变cat的属性值就可以实现动态传递数据了

注意事项:defineprops在之前的Vue版本中需要引入,但是现在是不需要了。上面几个例子是建立在setup语法糖的基础上编写的即<script setup lang="ts">,如果你不是太熟悉setup语法糖,那么就需要在script标签中使用setup(){}中使用props属性取得传递的数据

更多推荐

Vue3 props的使用详解

本文发布于:2024-03-10 04:36:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详解   props

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!