Vue3知识总结-4

Vue3知识总结-4

文章目录

  • Vue3知识总结-4
  • 插槽Slots
    • 渲染作用域
    • 默认内容
    • 具名插槽
    • 插槽中的数据传递
    • 具名插槽传递数据
  • 组件声明周期
    • 声明周期示意图
  • 组件生命周期的应用
  • 动态组件
  • 组件保持存活
    • 组件被卸载
  • 异步组件
  • 依赖注入

插槽Slots

在某些场景中,可能想要为子组件传递一些模版片段,让子组件在他们的组件中渲染这些片段

<template><SlotsBase><div><h3>插槽标题</h3><p>插槽内容</p></div></SlotsBase>
</template>
<script>
import SlotsBase from "@/components/SlotsBase.vue";
export default {components:{SlotsBase}
}
</script>
<style></style>

渲染作用域

插槽内容可以访问到父组件的数据作用域,因为插槽内容本心是在父组件模版中定义的

默认内容

如果没有传数据,可以有一个默认的值

具名插槽

插槽中的数据传递

需要同时使用父组件和子组件域内的数据。

具名插槽传递数据

<template><SlotsBase><div><h3>插槽标题</h3><p>插槽内容</p></div><template v-slot:header><h3>{123}</h3></template>
<!--    //简写的方式--><template #main><h3>{32131</h3></template><h3>{{message}}</h3><h3>{{currentTest}}</h3>
<!--    传递数据--><SlotsBase v-slot="slotProps"><h3>{{currentTest}}-{{slotProps.msg}}</h3></SlotsBase>
<!--    具名插槽传递数据--><SlotsBase><template #header="slotProps"><h3>{{currentTest}}-{{slotProps.msg}}</h3></template><template #main="slotProps"><h3>{{currentTest}}-{{slotProps.job}}</h3></template></SlotsBase></SlotsBase></template>
<script>
import SlotsBase from "@/components/SlotsBase.vue";
export default {data(){return{message:"插槽续集",currentTest:"测试内容"}},components:{SlotsBase}
}
</script>
<style></style>
<template><h3>base</h3><slot>插槽默认值</slot><hr><slot name="header">插槽默认值</slot><slot name="main">插槽默认值</slot><slot name="header" :msg="childMessage"></slot><slot name="main" :job="jobMsg"></slot>
</template><script>
export default {data(){return{childMessage:"子组件数据",jobMsg:"xiyou "}}
}
</script><style scoped></style>

组件声明周期

创建到销毁的生命周期

声明周期示意图

在这里插入图片描述

<template><h3>组件的生命周期</h3><p>{{message}}</p><button @click="updateHandle">更新数据</button>
</template>
<script>
//生命周期函数:
// 创建:beforeCreate created
// 挂载:beforeMount mounted
// 更新:beforeUpdate updated
// 销毁:beforeUnmount unmounted
export default {data(){return{message:"更新之前"}},methods:{updateHandle(){this.message = "更新之后"}},beforeCreate() {console.log("组件创建之前")},created() {console.log("组件创建之后")},beforeMount() {console.log("组件渲染之前")},mounted() {console.log("组件渲染之后")},beforeUpdate() {console.log("组件更新之前")},updated() {console.log("组件更新之后")},beforeUnmount() {console.log("组件销毁之前")},unmounted() {console.log("组件销毁之后")},}
</script>

组件生命周期的应用

  1. 通过ref获取元素DOM结构
  2. 迷你网络请求渲染数据
<template><h3>组件生命周期函数应用</h3><p ref="name">程序员</p><ul><li v-for="(item,index) of banner" :key="index"><h3>{{item.title}}</h3><p>{{item}}</p></li></ul>
</template><script>
export  default {data(){return{banner: []}},created() {this.banner = [{"title":"123","content":"342"},{"title":"123","content":"342"},{"title":"123","content":"342"},]},beforeMount() {console.log(this.$refs.name)},mounted() {console.log(this.$refs.name)},
}
</script><style scoped></style>

动态组件

<template><component :is="tabComponent"></component><button @click="changeHandler">切换组件</button>
<!--<ComponentA/>-->
<!--<ComponentB/>-->
</template><script>import ComponentA from "@/components/ComponentA.vue"
import ComponentB from "@/components/ComponentB.vue"
export default {data(){return {tabComponent:"ComponentA"}},components:{ComponentA,ComponentB},methods:{changeHandler(){this.tabComponent = this.tabComponent == "ComponentA" ? "ComponentB" : "ComponentA"}}
}
</script>

组件保持存活

当使用在多个组件切换时候,被切换掉的组件会被卸载,可以用过组件强制被切换掉的组件任然保持“存活状态”

组件被卸载

<keep-alive><component :is="tabComponent"></component>
</keep-alive>

异步组件

同步:多个功能,一个一个执行

异步:多个动能,同时运行

依赖注入

Provide 和inject只能由上往下传递

<template>
<h3>Child</h3><p>{{title}}</p><p>{{message}}</p>
</template><script >
export default {inject: ['message'],props:{title:{type:String},}
}</script><style></style>
<template>
<h3>祖宗</h3><Parent title="祖宗的财产"/>
</template>
<script>
import Parent from "@/components/Parent.vue";
export default {data(){return{message:"123"}} ,components:{Parent},// provide:{//   message:"213"// }provide(){return {message: this.message}}
}
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/704325.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

easyx快速入门1

1.基本说明 EasyX 是针对 C 的图形库&#xff0c;可以帮助 C/C 初学者快速上手图形和游戏编程。 比如&#xff0c;可以基于 EasyX 图形库很快的用几何图形画一个房子&#xff0c;或者一辆移动的小车&#xff0c;可以编写俄罗斯方块、贪吃蛇、黑白棋等小游戏&#xff0c;可以练…

SDL系列(四)—— 事件机制

事件循环 大多数多媒体程序依靠 事件系统 来处理输入。 SDL 为处理输入事件提供了灵活的 API 。 本质上&#xff0c; SDL 将来自设备&#xff08;如键盘&#xff0c;鼠标或控制器&#xff09;的输入记录为 事件 &#xff0c;将它们存储在 “ 事件队列 ”中。 您可以将此…

硬盘坏道如何检测和修复?

硬盘是我们储存数据的重要设备&#xff0c;然而在使用过程中&#xff0c;我们可能会遇到一些困扰&#xff0c;比如硬盘出现坏道的问题。那么&#xff0c;什么是坏道呢&#xff1f;硬盘出现坏道会对我们的性能和数据安全产生影响吗&#xff1f;如何去检测和修复这些坏道呢&#…

【STM32-MX_GPIO_Init分析】

MX_GPIO_Init分析源码如下&#xff1a; __HAL_RCC_GPIOE_CLK_ENABLE源码如下&#xff1a; #define RCC ((RCC_TypeDef *) RCC_BASE) #define RCC_BASE (AHB1PERIPH_BASE 0x3800UL) #define AHB1PERIPH_BASE (PERIPH_BASE 0x00020000U…

失业焦虑如何缓解心情?流静冥想

失业焦虑如何缓解心情&#xff1f;人生旅途&#xff0c;失业犹如山重水复&#xff0c;焦虑似迷雾遮望眼。古语云&#xff1a;“山不厌高&#xff0c;海不厌深。”心之向往&#xff0c;冥想便是那披荆斩棘之斧&#xff0c;如何带你走出困境&#xff1f; “静以修身”&#xff0c…

你是学会了还是学废了:Elasticsearch 7 集群拷贝到其它环境如何重置密码

欢迎您关注我的公众号【尚雷的驿站】 公众号&#xff1a;尚雷的驿站 CSDN &#xff1a;https://blog.csdn.net/shlei5580 墨天轮&#xff1a;https://www.modb.pro/u/2436 PGFans&#xff1a;https://www.pgfans.cn/user/home?userId4159 前言 本文描述了将生产ES集群打包拷贝…

实时美颜技术揭秘:直播美颜SDK的架构与优化

当下&#xff0c;美颜技术成为直播平台吸引用户和提升用户体验的重要手段。本文将揭秘实时美颜技术&#xff0c;详细介绍直播美颜SDK的架构&#xff0c;并探讨其优化方法。 一、实时美颜技术概述 1、发展历程 随着图像处理算法的进步&#xff0c;逐渐发展到实时视频处理领域…

文件系统和软硬连接

一、磁盘 磁盘可以存储大量的二进制数据&#xff0c;并且断电后也能保持数据不丢失。因此磁盘是一种永久性存储介质&#xff0c;在计算机中&#xff0c;磁盘是一个外设&#xff0c;也是唯一的机械设备。既然磁盘是一个外设&#xff0c;那么就意味着&#xff0c;磁盘和内存&…

Elasticsearch 搜索引擎实现对文档内容进行快速检索(保姆级教程)

本文主要讲解ES如何从提取文档中提取内容&#xff08;word、pdf、txt、excel等文件类型&#xff09;&#xff0c;实现快速检索文档内容实现。 特别说明一下&#xff0c;为什么用7.10.0版本&#xff0c;因为在项目中除了精确匹配的要求&#xff0c;也会有模糊查询&#xff08;关…

小程序蓝牙连接ESP32通信(可直接拿来用)

小程序中的蓝牙能力 在小程序中&#xff0c;要使用蓝牙能力&#xff08;Beacon 除外&#xff09;必须首先调用 wx.openBluetoothAdapter 初始化蓝牙适配器模块&#xff0c;其生效周期为调用 wx.openBluetoothAdapter 至调用 wx.closeBluetoothAdapter 或小程序被销毁为止。只有…

【mysql】mysql导入导出数据详解

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全…

翻译《The Old New Thing》- What does the CS_OWNDC class style do?

What does the CS_OWNDC class style do? - The Old New Thing (microsoft.com)https://devblogs.microsoft.com/oldnewthing/20060601-06/?p31003 Raymond Chen 2006年06月01日 简要 本文讨论了CS_OWNDC窗口类样式的影响&#xff0c;它让窗口管理器为窗口创建一个永久的设…