当前位置: 首页 > java >正文

Vue3 setup的两个注意点

一、执行时机

        在beforeCreate之前执行一次,this是undefined。

<script>
import Demo from './components/Demo.vue'
export default {name: 'App',components:{Demo},mounted() {console.log('-------mounted----------') //后输出},setup() {console.log('--------setup----------') //先输出}
}
</script>

二、参数

        1.props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。

        2.context:上下文对象

                (1).attrs:值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性,相当于 this.$attrs 

                (2).emit:分发自定义事件的函数, 相当于 this.$emit

                (3).slots:收到的插槽内容, 相当于 this.$slots

<template><h1>一个人的信息</h1><slot name="Hello"></slot><h2>姓名:{{ name }}</h2><h2>年龄:{{ age }}</h2><button @click="sendNameBtn">发送姓名</button><br /><br />
</template><script>
export default {name: "Demo",props:['name','age'],emits:['sendName'],setup(props,context) {// console.log(props)// console.log('@@@@',context)// console.log('@@@@',context.attrs['age']) //相当于vue2中的$attrs// console.log('@@@@',context.emit) //相当于vue2中的$emit,用于触发自定义事件console.log('@@@@',context.slots) //插槽function sendNameBtn(){context.emit('sendName',props.name)}return {sendNameBtn};},
};
</script>

        注意:在Vue3中具名插槽只能使用在template上。

    <template v-slot:Hello>

        <h1>你好啊!</h1>

    </template>

http://www.xdnf.cn/news/16890.html

相关文章:

  • Spring AI MCP 技术深度解析:从工具集成到企业级实战
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现道路车辆事故的检测识别(C#代码UI界面版)
  • LeeCode 88. 合并两个有序数组
  • RAGFLOW~Enable RAPTOR
  • 亚像素级精度的二维图像配准方法
  • 上海月赛kk
  • HCLP--ospf综合实验
  • RabbitMQ-镜像队列(Mirrored Queues)
  • VMware 下 Ubuntu 操作系统下载与安装指南
  • RAGFLOW~knowledge graph
  • Redis的ZipList、SkipList和ListPack之间的区别
  • 【Linux】重生之从零开始学习运维之Mysql
  • Au速成班-多轨编辑流程
  • STM32学习记录--Day5
  • 《C++》STL--list容器详解
  • EasyExcel 公式计算大全
  • 谷歌Firebase动态链接将失效:如何选择深度链接替代方案?
  • 11.Layout-Pinia优化重复请求
  • 51单片机入门:模块化编程
  • 利用 AI 在 iPhone 上实现 App 文本情绪价值评估(下)
  • 【string类常见接口】
  • 智能Agent场景实战指南 Day 28:Agent成本控制与商业模式
  • C语言(02)——标准库函数大全(持续更新)
  • Spring Boot + MongoDB:从零开始手动配置 MongoConfig 实战
  • C语言:冒泡排序
  • 【3】交互式图表制作及应用方法
  • kafka快速部署、集成、调优
  • 香港正式启动稳定币牌照制度!推动中国的人民币国际化?
  • 智能Agent场景实战指南 Day 29:Agent市场趋势与前沿技术
  • ALOcc: Adaptive Lifting-based 3D Semantic Occupancy and