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

Vue 3 快速入门 第六章

接上文,我继续讲述 Vue 3 中的组件的相关知识。

目录

组件 v-model

原理

作用

自定义组件上使用

插槽

作用

基本使用

渲染作用域

设置默认值

具名插槽

作用域插槽


组件 v-model

原理

v-model 是 Vue 提供的一个语法糖,用于在表单元素和组件上实现双向数据绑定。它的本质是一个 value 属性绑定和一个事件监听的组合。

<input type="text" v-model="message"><input :value="message"@input="message = $event.target.value"
>

作用

提供数据的双向绑定

数据变,视图跟着变 :value

视图变,数据跟着变 @input

$event用于在模板中,获取事件的形参

自定义组件上使用

在 Vue 3 中,自定义组件上使用v-model,相当于使用modelValue属性,同时触发 updata:modelValue 事件

<MyTest v-model="isVisible">
// 相当于
<MyTest :modelValue="isVisible" @update:modelValue="isVisible=$event">

我们需要先定义props,再定义emits。其中有许多重复的代码。如果需要修改此值,还需要手动调用emits值,非常麻烦。 

<!-- 父组件 -->
<script setup lang="ts">
import MyInput from './components/my-input.vue';
import { ref } from 'vue'const txt = ref('123456')
</script><template><div><MyInput v-model="txt"></MyInput>{{ txt }}</div>
</template><style scoped></style>
<!-- 子组件 -->
<template><div><input type="text" :value="modelValue" @input="e => emit('update:modelValue', (e.target as HTMLInputElement).value)" /></div>
</template><script setup lang="ts">
defineProps({modelValue: String
})
const emit = defineEmits(['update:modelValue'])
</script><style scoped></style>

从 Vue 3.4 开始,推荐的实现方式是使用 defineModel() 宏:

<template><div><input type="text" :value="modelValue" @input="e => modelValue = (e.target as HTMLInputElement).value" /></div>
</template><script setup lang="ts">
import { defineModel } from 'vue'
const modelValue = defineModel()
</script><style scoped></style>

defineModel() 返回的值是一个 ref。它可以像其他 ref 一样被访问以及修改,不过它能起到在父组件和当前变量之间的双向绑定的作用:

  • 它的 .value 和父组件的 v-model 的值同步;
  • 当它被子组件变更了,会触发父组件绑定的值一起更新。

本质原理还是和上面的一样。 

插槽

作用

让组件内部的一些结构支持自定义

基本使用

1.组件内部需要定制的结构部分,改用<slot></slot>占用

2.使用组件时,组件标签内部传入结构代替solt

<FancyButton>Click me! <!-- 插槽内容 -->
</FancyButton>
<button class="fancy-btn"><slot></slot> <!-- 插槽出口 -->
</button>

渲染作用域

插槽内容可以访问到父组件的数据作用域,但是无法访问子组件的数据。

设置默认值

在<slot>标签内可以放置内容作为默认内容

<button type="submit"><slot>Submit <!-- 默认内容 --></slot>
</button>

具名插槽

作用:定制组件内多处内容

语法:

废话不多说,直接上代码

<div class="container"><header><slot name="header"></slot></header><main><slot></slot></main><footer><slot name="footer"></slot></footer>
</div>
<BaseLayout><template v-slot:header><!-- header 插槽的内容放这里 --></template>
</BaseLayout>

没有提供 name 的 <slot> 出口会隐式地命名为“default”

v-slot 有对应的简写 #,因此 <template v-slot:header> 可以简写为 <template #header>。

当一个组件同时接收默认插槽和具名插槽时,所有位于顶级的非 <template> 节点都被隐式地视为默认插槽的内容。

<BaseLayout><template #header><h1>Here might be a page title</h1></template><template #default><p>A paragraph for the main content.</p><p>And another one.</p></template><template #footer><p>Here's some contact info</p></template>
</BaseLayout>

也就是说,上面的这一段可以简化为下面这一段。

<BaseLayout><template #header><h1>Here might be a page title</h1></template><!-- 隐式的默认插槽 --><p>A paragraph for the main content.</p><p>And another one.</p><template #footer><p>Here's some contact info</p></template>
</BaseLayout>

作用域插槽

允许子组件向父组件传递数据,使父组件可以自定义如何渲染这些数据。

<!-- 子组件 ChildComponent.vue -->
<template><div><slot :user="user" :age="age"></slot></div>
</template><script setup>
const user = ref('张三')
const age = ref(25)
</script><!-- 父组件使用 -->
<ChildComponent v-slot="slotProps">用户名:{{ slotProps.user }},年龄:{{ slotProps.age }}
</ChildComponent>
http://www.lryc.cn/news/617239.html

相关文章:

  • Linux操作系统从入门到实战(十九)进程状态
  • JS-第二十三天-正则
  • 大数据量下分页查询性能优化实践(SpringBoot+MyBatis-Plus)
  • 集成电路学习:什么是URDF Model统一机器人描述格式模型
  • ZeroNews:如何构建安全(无需 V*N!)的工业物联网连接
  • 大模型落地:AI 技术重构工作与行业的底层逻辑
  • Salesforce案例:零售企业会员积分体系
  • 【软考架构】需求工程中,系统分析与设计的结构化方法
  • [Shell编程] Shell 编程之免交互
  • C语言模拟 MCU 上电后程序的执行顺序 + 回调函数机制 + 程序计数器(PC)和堆栈的作用
  • LangVM —— 一站式多语言版本管理工具,让 Java、Python、Go、Node.js 切换更丝滑
  • CVE-2019-0708复刻
  • buildroot编译qt 5.9.8 arm64版本踩坑
  • Windows文件时间修改指南:从手动到自动化
  • AI驱动的智能编码革命:从Copilot到全流程开发自动化
  • FFmepg源码系列-avformat_open_input()
  • Python调用C/C++函数库的多种方法与实践指南
  • 聊天室全栈开发-保姆级教程(Node.js+Websocket+Redis+HTML+CSS)
  • MathType关联Wps实现公式编辑【Tex语法适配】
  • 2438. 二的幂数组中查询范围内的乘积
  • 【liunx】web高可用---nginx
  • 编译Android版本可用的高版本iproute2
  • 机器学习 - Kaggle项目实践(1)Titanic
  • C++多态详解
  • SDI设计中,为何SD-SDI模式下,接收器用DRU实现,在3G-SDI模式下,使用transceiver实现
  • 多轮会话记忆的核心挑战
  • Spring Boot 中 @Transactional 解析
  • 自动化备份全网服务器数据平台项目
  • P2865 [USACO06NOV] Roadblocks G
  • ListNode* dummy = new ListNode();什么意思