VUE3中的组件通信
工作中使用组件之间传值在此记录
目录
VUE3中的组件通信六种方法介绍与基本使用
一、父传子(props)
二、 Emits 传值(子组件向父组件传值)
三、v-model 双向绑定
四、 provide/inject(跨层级组件传值)
五、事件总线(mitt 库)第三方库
六、 Vuex 或 Pinia(状态管理库)
VUE3中的组件通信六种方法介绍与基本使用
一、父传子(props)
Props 是最常用的父组件向子组件传递数据的方式。父组件通过属性绑定将数据传递给子组件,子组件使用 defineProps 来接收数据。
父组件
<template>
<ChildComponent :list="list" />
</template>
<script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';
const list= ref([
{name:“我是测试数据”}
]);
</script>
子组件
```bash
<template>
<p>{{ list[0].name}}</p>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
list: {
type: Arrary,
default: []
}
});
</script>```
二、 Emits 传值(子组件向父组件传值)
子组件可以通过 defineEmits 定义自定义事件,使用 emit 触发事件并传递数据给父组件。多用于表单提交等
父组件
<template>
<ChildComponent @customEvent="handleChildEvent" />
</template>
<script setup>
import ChildComponent from './ChildComponent.vue';
const handleChildEvent = (data) => {
console.log('Received data from child:', data);
};
</script>
子组件
1.通过点击触发emit传值
<template>
<button @click="sendDataToParent">Send Data</button>
</template>
<script setup>
import { defineEmits } from 'vue';
const emits = defineEmits(['customEvent']);
//const emits = defineEmits('customEvent'); 这种写法也可以 但是必须defineEmits先定义函数
const sendDataToParent = () => {
emits('customEvent', 'Data from child');
};
// emits(父组件函数名,参数)
</script>
三、v-model 双向绑定
v-model 是一种简化的语法糖,用于实现父组件和子组件之间的双向数据绑定。
父组件
<template>
<ChildComponent v-model="parentValue" />
<p>{{ parentValue }}</p>
</template>
<script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';
const parentValue = ref('Initial value');
</script>
子组件
<template>
<input v-model="localValue" @input="updateValue">
</template>
<script setup>
import { defineProps, defineEmits } from 'vue';
const props = defineProps({
modelValue: String
});
const emits = defineEmits(['update:modelValue']);
const localValue = ref(props.modelValue);
const updateValue = () => {
emits('update:modelValue', localValue.value);
};
</script>
、、、、、、、、、、、、、、、
或者
<template>
<div class="childPage">
<button @click="updateA">按钮</button>
</div>
</template>
<script lang="ts" setup>
import { defineModel } from "vue";
const parentValue = defineModel({
type: Number,
default: 0,
});
const updateA = () => {
parentValue value += 1;
};
</script>
四、 provide/inject(跨层级组件传值)
此种方式,可以用于替代 window.location.reload 刷新页面以此规避页面闪白问题
provide 和 inject 用于在祖先组件和后代组件之间进行跨层级的数据传递,不需要一层一层地通过 Props 传递。 不管嵌套的再深都可以使用,但要注意,只适用父子组件,无嵌套关系的不能使用
例如:
父组件
const isRouterJump= ref(true); // 是否路由跳转
const reload = () => {
isRouterJump.value = false;
nextTick(() => {
isRouterJump.value = true
})
}
provide("reload", reload);
<template>
<ConfigGlobal >
<RouterView v-if="isRouterJump"/>
<routerSearch />
</ConfigGlobal>
</template>
子组件
<template>
<p>{{ injectedData }}</p>
</template>
<script setup>
import { inject } from 'vue';
const reload = inject<any>("reload");
reload ()
</script>
五、事件总线(mitt 库)第三方库
篇幅有限 在这里只介绍用法
使用第三方库 mitt 可以创建一个全局的事件总线,实现任意组件之间的通信。
事件总线
创建 useEmitt.js
import mitt from 'mitt';
const emitter = mitt();
export default emitter;
发送事件的组件示例:
<template>
<button @click="sendEvent">Send Event</button>
</template>
<script setup>
import emitter from './eventBus';
const sendEvent = () => {
emitter.emit('customEvent', 'Data from sender');
};
</script>
接收事件的组件示例:
<template>
<!-- 组件模板 -->
</template>
<script setup>
import { onMounted, onBeforeUnmount } from 'vue';
import emitter from './eventBus';
const handleEvent = (data) => {
console.log('Received data:', data);
};
onMounted(() => {
emitter.on('customEvent', handleEvent);
});
onBeforeUnmount(() => {
emitter.off('customEvent', handleEvent);
});
</script>
六、 Vuex 或 Pinia(状态管理库)
Pinia 示例:
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0
}),
actions: {
increment() {
this.count++;
}
}
});
使用 Store 的组件示例:
<template>
<p>{{ counterStore.count }}</p>
<button @click="counterStore.increment">Increment</button>
</template>
<script setup>
import { useCounterStore } from './store';
const counterStore = useCounterStore();
</script>
VUE3中的组件通信的更多相关文章
- vue学习笔记(九)vue-cli中的组件通信
前言 在上一篇博客vue学习笔记(八)组件校验&通信中,我们学会了vue中组件的校验和父组件向子组件传递信息以及子组件通知父组件(父子组件通信),上一篇博客也提到那是对组件内容的刚刚开始,而本 ...
- Vue3 中的组件 provide和inject 传值、获取组件实例的方法getCurrentInstance()
一. provide和inject(依赖注入) 1:在父级组件中提供数据 语法:provide('提供给子组件的变量名',提供给子组件的数据) 2: 在子级组件中获取收据 ...
- vue 中的组件通信
vue中组件通信,一般分为三种情况,父与子,子与父,子子之间. 一.父与子通信 父组件将值传给子组件,一般通过props,设置默认的类型.调用的时候通过 xx=" ", 或者:XX ...
- Vue2中父子组件通信的几种常用方法
源码地址 点击查看演示源码 Vue2父子传参:props 首先在父组件中引入子组件,然后以属性的方式将数据传递给子组件 父组件: <template> <div class=&quo ...
- vue2.0 $emit $on组件通信
在vue1.0中父子组件通信使用$dispatch 和 $broadcast,但是在vue2.0中$dispatch 和 $broadcast 已经被弃用. 因为基于组件树结构的事件流方式实在是让人难 ...
- Vue3.x 关于组件的那些变化(新手必看篇)
一.组件内的 data 为什么总是函数形式? 我们试着先做一个计数器案例,把 data 的返回形式修改成一个对象.具体的代码如下: <template> <div> <b ...
- Vue中组件通信的几种方法(Vue3的7种和Vue2的12种组件通信)
Vue3组件通信方式: props $emit expose / ref $attrs v-model provide / inject Vuex 使用方法: props 用 props 传数据给子组 ...
- android中四大组件之间相互通信
好久没有写有关android有关的博客了,今天主要来谈一谈android中四大组件.首先,接触android的人,都应该知道android中有四大组件,activity,service,broadca ...
- React中嵌套组件与被嵌套组件的通信
前言 在React项目的开发中经常会遇到这样一个场景:嵌套组件与被嵌套组件的通信. 比如Tab组件啊,或者下拉框组件. 场景 这里应用一个最简单的Tab组件来呈现这个场景. import React, ...
- vue2.0s中eventBus实现兄弟组件通信
在vue1.0中,组件之间的通信主要通过vm.$dispatch沿着父链向上传播和用vm.$broadcast向下广播来实现.然而在vue2.0中,已经废除了这种用法. vuex加入后,对组件之间的通 ...
随机推荐
- 发那科FANUC机器人A06B-0652-B212电机维修基本流程
发那科FANUC机器人以其卓越的性能和可靠性赢得了广泛的认可.然而,就像其他任何机械设备一样,长时间的运行和复杂的工作环境都可能使伺服电机面临维修的需求.为了确保您的发那科FANUC机器人A06B-0 ...
- linux rpm包下载
下载地址 pkg.org 下载方法 搜索包名 找到需要的包 点击去,找到Download wget [Download url] 没有包管理器,恶心,真他妈恶心,我都不想使,还说啥稳定,环境都配不起来 ...
- MT Photos——一个比群晖Moments更好用的AI相册管理神器
MT Photos是一款为NAS用户量身打造的照片管理系统. 通过AI技术,自动将您的照片整理.分类,包括但不限于时间.地点.人物.照片类型. 您可以在任何支持Docker的系统中运行它. 如果您的操 ...
- Ansible - [06] Playbook
Playbook 概述 Ansible ad-hoc 可以通过命令行形式远程管理其他主机 适合执行一些临时性简单任务 Ansible playbook 中文名称叫 剧本 将经常需要执行的任务写入一个文 ...
- Week09_day05(Java API操作Hbase)
package com.wyh.HbaseAPI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbas ...
- CF1793E题解
\(\text{Problem - 1793E - Codeforces}\) \(\text{*2600}\) 备注 2024.10.19 考试 T2.考场未能想出正解,找到性质但没有根据性质往 d ...
- 「一」nginx介绍
应用场景 静态资源(js.css.图片 ) 反向代理 缓存加速(动态资源),比如社区活跃度排名 负载均衡(动态扩容.容灾) API服务 一个请求先经过nginx,再到应用服务器,访问数据库/redis ...
- golang定时器函数 每隔几分钟执行一个函数
延时调用 AfterFunc go function() func function() { // TODO 具体逻辑 // 每5分钟执行一次,递归调用自己 time.AfterFunc(5*time ...
- oracle的IP访问列表
Windows版本Oracle 19c. 在sqlnet.ora中添加下面语句 tcp.validnode_checking=yes tcp.invited_nodes=(127.0.0.1,132. ...
- 可视化|MapBoxGL
注册Proton Mail 用户名 密码 人机验证 昵称 设置恢复方法-选择稍后再说-确定 注册MapBoxGL 填写后 Finish creating your account 一开始之前记得选择U ...