vue 子传父,子组件向父组件传值,方法示例

vue 子传父,子组件向父组件传值,方法示例

上一篇介绍父传子props用法,这一篇介绍子传父 e m i t 思 路 过 程 : 子 组 件 通 过 执 行 事 件 利 用 emit 思路过程:子组件通过执行事件利用 emitemit发送事件名和需要传递的值;在父组件里面,在子组件的标签上绑定事件名,然后执行事件,将事件的形参赋值出来。

子组件

 // 子组件
<template>
 	<div class="childDiv">
 		// 假设需要点击按钮向父组件传值
 		<button @click="sendVal"></button>
	</div>
</template>

<script>
	export default {
		data () {
			return {
				val: '123123'
			}
		},
		methods: {
			// 点击事件,change事件也是一样的写法
			sendVal () {
				// 使用$emit发送事件
				// 第一个参数是发送的事件名,第二个参数是你需要传递的值
				this.$emit("sendMethod", this.val)
			}
		}
	}
</script>

父组件

 // 父组件
<template>
 	<div class="fatherDiv">
 		// 在子组件标签上绑定事件名
 		<ChildDiv @sendMethod="getMthodVal" />
	</div>
</template>
<script>
	// 引入子组件
	import ChildDiv from "./components/childDiv"
	export default {
		data () {
			return {
				fatherVal: null
			}
		},
		// 注册childDiv组件
		components: {
			ChildDiv
		},
		mounted () {
			// 执行getMthodVal方法
			this.getMthodVal()
		},
		methods: {
			// 写入方法
			// 参数val是传递过来的值
			getMthodVal(val) {
				this.fatherVal= val
			}
		}
	}
</script>

子传父就是这么简单,纯手打。谢谢!!~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,父组件组件传值有多种方法。其中一种方法是使用`defineProps`,在父组件中定义props属性,然后将需要传递给组件的值作为props的属性值。在组件中,可以使用`props`接收父组件传递的值。 具体实现步骤如下: 1. 在父组件中使用`defineProps`定义props属性,并将需要传递给组件的值作为props的属性值。 2. 在组件中使用`props`接收父组件传递的值。 下面是一个示例代码,展示了如何在Vue3中实现父向组件传值: ```javascript // 父组件 Father.vue <template> <div class="fa"> <div style="margin: 10px;">我是父组件</div> 父组件接收组件传的值:{{ sonMessage }} <Son :message="sonMessage"></Son> </div> </template> <script setup lang="ts"> import Son from './Son.vue' import { ref } from "vue"; const sonMessage = ref<string>(""); </script> <style scoped> .fa { border: 3px solid cornflowerblue; width: 400px; text-align: center; } </style> ``` ```javascript // 组件 Son.vue <template> <div class="son"> <div style="margin: 10px;">我是组件</div> 组件接收父组件传的值:{{ message }} </div> </template> <script setup lang="ts"> import { defineProps } from "vue"; const props = defineProps({ message: String, // 定义props属性,接收父组件传递的值 }); </script> <style scoped> .son { border: 3px solid lightgreen; width: 200px; text-align: center; } </style> ``` 在父组件中,使用`:message="sonMessage"`将`sonMessage`的值传递给组件的`message` props属性。在组件中,使用`props`接收父组件传递的值,并在模板中展示。 这样,就实现了在Vue3中父组件组件传递值的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Vue3父组件间传参通信](https://blog.csdn.net/qq_45397526/article/details/126281133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3 父组件组件如何传值 详解](https://blog.csdn.net/qq_56263094/article/details/124576055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值