setup 语法糖

本文介绍了Vue.js中script setup语法糖的使用,包括接收props、声明事件、访问插槽和属性、导出子组件属性以及顶层await的处理。重点讲解了defineProps、defineEmit、useSlots、useAttrs和defineExpose在组件交互中的作用。
摘要由CSDN通过智能技术生成

**

1.语法

**

<template>
	<Foo></Foo>
</template>
<script setup>
  //导入任意的组件就可以直接在 template 中使用
 import Foo from "./components/Foo"

</script>

2. defineProps 用来接收父组件传来的props defineEmit 用来声明触发的事件

//子组件 msg.vue
<template>
  <div>
    {
   {
    msg }}
    <button @click="onClick">点击按钮</button>
  </div>
</template>
<script setup>
  import {
    defineProps , defineEmits  } from 'vue'
  let props = defineProps({
   
 		 msg: String,
	});
	console.
Vue的setUp语法糖是一种简化Vue组件代码的语法形式。它使用了单文件组件的方式,将模板、脚本和样式放在同一个文件中,使得组件更加易读和易维护。 在setUp语法糖中,使用<script setup>标签来定义组件的逻辑部分。在这个标签内,我们可以使用defineProps函数来声明props,并且具备完整的类型推断。比如可以这样声明props:const props = defineProps({ foo: String })。我们也可以使用withDefaults函数来为props设置默认值,比如const props = withDefaults(defineProps<Props>(), { msg: 'hello', labels: () => ['one', 'two'] })。 同时,在<script setup>标签中,我们可以直接使用emit函数来触发父组件的事件监听器,而不需要使用this.$emit。这样可以简化代码的书写。 在模板中,我们可以直接使用组件的标签名来引入自定义组件,而不需要在component对象里进行声明。比如<template> <MyComponent /> </template>,然后在<script setup>中import MyComponent from './MyComponent.vue'。 总的来说,Vue的setUp语法糖能够简化组件的编写和维护,使得代码更加清晰和易读。它提供了更加简洁的语法形式来定义props、触发事件等功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3中setup语法糖学习](https://blog.csdn.net/pdd11997110103/article/details/121438090)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值