VUE3中的组件传值

一、父传子(props)

在子组件中可以使用defineProps接收父组件向子组件的传值

父组件fatherPage.vue:

<template><div class="father"><button @click="a = a + 1">按钮</button><childPage :a="a" /></div>
</template><script lang="ts" setup>
import { ref } from "vue";
import childPage from "./childPage.vue";
const a = ref<number>(0);
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

子组件childPage.vue:

<template><div class="childPage">{{ a }}</div>
</template>
<script lang="ts" setup>
import { computed, defineProps } from "vue";
// 含默认值
// const porps = defineProps({
//   a: {
//     type: Number,
//     default: 0,
//   },
// });
const props = defineProps({ a: Number });
// 用computed使用props
const a = computed(() => props.a);
</script>
<style scoped lang="less">
.childPage {width: 100px;height: 60px;background-color: rgba(0, 0, 0, 0);display: flex;justify-content: center;align-items: center;color: #ffffff;
}
</style>

效果:

二、子传父(emits)

在子组件中可以使用defineEmits向父组件传递信息。

父组件fatherPage.vue:

<template><div class="father">{{ a }}<childPage @update:a="update" /></div>
</template><script lang="ts" setup>
import { ref } from "vue";
import childPage from "./childPage.vue";
const a = ref<number>(0);
const update = (emitValue: number) => {a.value = emitValue;
};
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

子组件childPage.vue:

1.通过点击触发emit传值

<template><div class="childPage"><button @click="updateA">按钮</button></div>
</template><script lang="ts" setup>
import { ref, defineEmits } from "vue";const a = ref<number>(0);
const emit = defineEmits(["update:a"]);
const updateA = () => {a.value++;emit("update:a", a.value);
};
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

2.通过watch监听值的变化进行传值:

<template><div class="childPage"><button @click="updateA">按钮</button></div>
</template><script lang="ts" setup>
import { ref, defineEmits, watch } from "vue";const a = ref<number>(0);
const emit = defineEmits(["update:a"]);
const updateA = () => {a.value++;setTimeout(() => {updateA();}, 1000);
};
watch(a, (newValue) => {emit("update:a", newValue);
});
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

三、父子组件进行双向绑定(v-model)

在子组件中可以使用defineModel与父组件进行双向绑定。注意:defineModel在vue3.3版本实验性使用,稳定版在vue3.4。

父组件:

<template><div class="father">{{ a }}<childPage v-model="a" /></div>
</template><script lang="ts" setup>
import { ref } from "vue";
import childPage from "./childPage.vue";
const a = ref<number>();
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

子组件:

<template><div class="childPage"><button @click="updateA">按钮</button></div>
</template><script lang="ts" setup>
import { defineModel } from "vue";const a = defineModel({type: Number,default: 0,
});const updateA = () => {a.value += 1;
};
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

四、使用vuex进行组件通信

使用vuex可以进行组件之间的通信,父子组件与兄弟组件均可

在vuex文件index.ts中:

import { createStore } from "vuex";export default createStore({state: {number: 0,},getters: {},mutations: {changeNumber(state, payload) {state.number = payload;},},actions: {},modules: {},
});

在第一个组件HomeView中:

<template><div class="father">{{ a }}<AboutView /></div>
</template><script lang="ts" setup>
import { computed } from "vue";
import { useStore } from "vuex";
import AboutView from "./AboutView.vue";
const store = useStore();
const a = computed(() => store.state.number);
</script>
<style scoped lang="scss">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;
}
</style>

在第二个组件AboutView中:

<template><div class="childPage"><button @click="updateA">按钮</button></div>
</template><script lang="ts" setup>
import { useStore } from "vuex";const store = useStore();
let a = 0;const updateA = () => {a++;store.commit("changeNumber", a);
};
</script><style scoped lang="scss">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

五、使用pinia进行组件通信

pinia有着与vuex类似的功能,同样可以实现组件间的通信。

在store的index.ts文件中:

import { defineStore } from "pinia";
import { ref } from "vue";
export const useCounterStore = defineStore("counter", () => {const count = ref(0);const changeCount = () => {count.value++;};return { count, changeCount };
});

在第一个组件fatherPage中:

<template><div class="father">{{ count }}<childPage /></div>
</template><script lang="ts" setup>
import childPage from "./childPage.vue";
import { useCounterStore } from "../store/index";
import { storeToRefs } from "pinia";
const store = useCounterStore();
const { count } = storeToRefs(store);
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

在第二个组件childPage中:

<template><div class="childPage"><button @click="changeCount">按钮</button></div>
</template><script lang="ts" setup>
import { useCounterStore } from "../store/index";
const store = useCounterStore();
const { changeCount } = store;
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

六、使用事件总线进行组件通信

我们可以利用第三方库比如说mitt以事件总线的方式实现传值

在mitt的index.ts中:

import mitt from "mitt";
const emitter = mitt();
export default emitter;

在第一个组件fatherPage中:

<template><div class="father">{{ count }}<childPage /></div>
</template><script setup>
import childPage from "./childPage.vue";
import emitter from "@/mitt/index";
import { ref } from "vue";
const count = ref(0);
emitter.on("changeCount", (counter) => {count.value = counter;
});
</script>
<style scoped lang="less">
.father {width: 100%;height: 100vh;background-color: rgb(7, 14, 17);color: #ffffff;display: flex;justify-content: center;align-items: center;button {width: 100px;height: 60px;cursor: pointer;}
}
</style>

在第二个组件childPage中:

<template><div class="childPage"><button @click="changeCount">按钮</button></div>
</template><script setup>
import emitter from "../mitt/index";
let a = 0;
const changeCount = () => {a++;emitter.emit("changeCount", a);
};
</script><style scoped lang="less">
.childPage {width: 100px;height: 60px;button {width: 100%;height: 100%;cursor: pointer;}
}
</style>

效果:

七、更详细使用方式请参考以下文档

1,Vue.js - 渐进式 JavaScript 框架 | Vue.js

2,开始 | Vuex

3,Pinia | Pinia 中文手册 - 官网同步更新 v2.0.28

4,GitHub - developit/mitt: 🥊 Tiny 200 byte functional event emitter / pubsub.

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

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

相关文章

clickhouse的多路径存储策略

存储策略 clickhouse从19.15开始&#xff0c;MergeTree实现了自定义存储策略的功能&#xff1a; JBOD策略&#xff1a;这种策略适合服务器挂多磁盘但没做raid的场景。JBOD是一种轮询策略&#xff0c;每次执行INSERT或者MERGE&#xff0c;所以产生的新分区会轮询写入各个磁盘。…

C#面:Application , Cookie 和 Session 会话有什么不同

Application、Cookie 和 Session 是在Web开发中常用的三种会话管理方式 Application&#xff08;应用程序&#xff09;&#xff1a; Application 是在服务器端保存数据的一种方式&#xff0c;它可以在整个应用程序的生命周期内共享数据。Application 对象是在应用程序启动时创…

Nginx 隐藏版本信息和logo

1.隐藏版本信息 http {### 隐藏版本号 server_tokens off; } 2.隐藏图标 2.1 cd nginx 安装的路径 cd/XXXX/nginx-1.2.0 2.2 编辑文件 vim src/core/nginx.h 修改define nginx_ver 中的内容 vim src/http/ngx_http_special_response.c 修改 u_char ngx_http_error_tail[]…

java 基础(核心知识搭配代码)

前言 java的学习分为了上部分以及下部分进行学习&#xff0c;上部分就是对于java的基础知识&#xff0c;面向对象上&#xff0c;面向对象下&#xff0c;异常操作&#xff0c;javaApi&#xff1b;下部主要是集合&#xff0c;泛型&#xff0c;反射&#xff0c;IO流&#xff0c;J…

Grid-Based Continuous Normal Representation for Anomaly Detection 论文阅读

Grid-Based Continuous Normal Representation for Anomaly Detection 论文阅读 摘要简介方法3.1 Normal Representation3.2 Feature Refinement3.3 Training and Inference 4 实验结果5 总结 文章信息&#xff1a; 原文链接&#xff1a;https://arxiv.org/abs/2402.18293 源码…

ChatGPT4.0使用次数限制解读

ChatGPT4.0使用次数限制解读 ChatGPT4.0简介 ChatGPT4.0&#xff0c;由OpenAI开发的先进通用聊天机器人模型&#xff0c;基于GPT4技术构建&#xff0c;为用户提供了自然语言处理等多项任务的解决方案。 ChatGPT4.0使用次数限制 在日常使用过程中&#xff0c;用户会遇到Chat…

【MIT 6.S081】2020, 实验记录(6),Lab: Copy-on-Write Fork

目录 Task: Implement copy-on writestep 1&#xff1a;对内存块进行引用计数step 2&#xff1a;uvmcopy 实现 fork 时将 parent 的物理页映射到 child 中step 3&#xff1a;在 usertrap 中增加对 page fault 的处理执行测试 官方说明&#xff1a;Lab: Copy-on-Write Fork for …

IP地址工具,判断IP是否在指定范围内(支持ipv6)

常用方法&#xff0c;判断一个ip是否在指定的ip范围内&#xff0c;范围可能包括起始ip范围或者掩码形式&#xff0c;无其它依赖&#xff0c; package com.yk.ip;import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; import jav…

操作系统-文件原理

目录 一、磁盘 1.1 磁盘结构 1. 盘片&#xff1a; 2. 盘面&#xff1a; 3. 磁头&#xff1a; 4. 磁道&#xff1a; 5. 扇区&#xff1a; 6. 磁道密度和扇区密度&#xff1a; 1.2 磁盘访问 1. 寻道&#xff08;Seeking&#xff09;&#xff1a; 2. 延迟旋转&#xff…

C++进阶-- map和set

关联式容器 在前面&#xff0c;我们所学的vector、list、deque&#xff0c;这些都是序列容器&#xff0c;也就是底层为线性序列的数据结构。 而关联式容器是C标准库中的一种类别&#xff0c;用于存储键值对&#xff08;key-value pair&#xff09;&#xff0c;关联式容器中的元…

vxe-table编辑单元格动态插槽slot的使用

业务场景&#xff1a;表格中只有特定某一行的的单元格可以编辑&#xff0c;列很多&#xff0c;为每个列写个插槽要写很多重复代码&#xff0c;所以这里使用动态插槽&#xff0c;简化代码量。显示编辑图标&#xff0c;点击编辑图标隐藏。失去焦点保存调后台接口。 解决办法&…

Linux多线程服务端编程:使用muduo C++网络库 学习笔记 附录C 关于Boost的看法

这是作者为电子工业出版社出版的《Boost程序库完全开发指南》写的推荐序&#xff0c;此处节选了作者对在C工程项目中使用Boost的看法。 最近一年&#xff08;这篇文章写于2010年8月&#xff09;作者电话面试了数十位C应聘者。惯用的暖场问题是“工作中使用过STL的哪些组件&…

十行代码开发一个AI应用

Semantic Kernal 简介 Semantic Kernel (SK) is a lightweight SDK that lets you easily mix conventional programming languages with the latest in Large Language Model (LLM) AI "prompts" with templating, chaining, and planning capabilities out-of-the-…

关于vue中关于eslint报错的问题

1 代码保存的时候会自动将单引号报错为双引号 导致eslint报错的问题&#xff0c; 解决思路&#xff1a; 在项目根目录下新建一个.prettierrc.json文件 { “tabWidth”: 2,“useTabs”: false,“singleQuote”: true,“semi”: false} 2 关于报错代码的时候 出现尾随逗号报错…

Zabbix 系统告警“More than 75% used in the configuration cache”处理办法

Zabbix系统报错提示 Zabbix 系统告警“More than 75% used in the configuration cache”&#xff0c;看了一下意思是可用的配置缓存超过75%。 修改缓存大小 vim /etc/zabbix/zabbix_server.confesc : /CacheSize 找到配置 将64M改大一点&#xff0c;保存退出。 重启zabbix…

WPF常用mvvm开源框架介绍 vue的mvvm设计模式鼻祖

WPF&#xff08;Windows Presentation Foundation&#xff09;是一个用于构建桌面应用程序的.NET框架&#xff0c;它支持MVVM&#xff08;Model-View-ViewModel&#xff09;架构模式来分离UI逻辑和业务逻辑。以下是一些常用的WPF MVVM开源框架&#xff1a; Prism Prism是由微软…

代码随想录算法训练营 Day32 | LeetCode122.买卖股票的最佳时机II、LeetCode55. 跳跃游戏、LeetCode45.跳跃游戏II

LeetCode122.买卖股票的最佳时机II 那么这里面根据贪心思想&#xff1a; 1、在最低点时买入&#xff0c;如果该点比上一点价格更低&#xff0c;只有两种情况&#xff1a;上一点为买入点&#xff0c;则此时更新买入点&#xff1b;上一点不是买入点&#xff0c;则此时将股票卖出…

物联网的核心技术有哪些?

物联网的核心技术有哪些? 说起物联网的相关技术&#xff0c;涉及到很多&#xff0c;比如自动识别技术、传感器技术、网络通信技术、云计算等&#xff0c;但说到核心技术&#xff0c;我认为有以下6个。这6个核心技术分别是感知和识别技术、物联网设备硬件、通信技术、数据处理技…

【MySQL】数据库中常用的函数

目录 聚合函数COUNT()函数的多种用法COUNT(*)COUNT(主键)COUNT(1)COUNT(常量)COUNT(非主键)COUNT(distinct(字段)) COUNT()函数小结 字符函数length(str)函数&#xff1a;获取参数值的字节个数concat(str1,str2,...)函数&#xff1a;字符串拼接upper(str)、lower(str)函数:大小…

Linux高负载排查最佳实践

在Linux系统中&#xff0c;经常会因为负载过高导致各种性能问题。那么如何进行排查&#xff0c;其实是有迹可循&#xff0c;而且模式固定。 本次就来分享一下&#xff0c;CPU占用过高、磁盘IO占用过高的排查方法。 还是那句话&#xff0c;以最佳实践入手&#xff0c;真传一句话…