vue组件间传值的方法你知道几种
作者:最好的我们, 时间:2024-04-28 09:31:32
一丶父子组件传值
先在父组件中给子组件的自定义属性绑定一个 父组件的变量
<template class="father">
<child :自定义属性名="父组件的变量"></child>
<template >
在子组件的props属性中可以取出父组件给的值,props中的变量用法和data中变量用法完全一样,只不过值的来源不同
export default {
name: "child",
props: ["自定义属性名"],
data() {}
}
二丶子父组件传值
先在父组件中给子组件的 自定义属性 绑定一个父组件的函数
<template class="father">
<child @自定义事件="父的处理函数">
<template >
export default {
name: "father",
data() {}
methods:{
父的处理函数(参数){
//参数:得到子组件触发事件($emit)时,传递过来的数据
}
}
})
在子组件中 this.$emit(“父的处理函数”,this.数据) 触发父组件中的函数进行传参
三丶兄弟组件传值
事件总线:
就是创建一个事件中心,相当于中转站,可以用它来传递事件和接收事件
创建全局空Vue实例:eventBus
import Vue from 'vue';
const eventBus= new Vue() //创建事件总线
export default eventBus;
具体页面使用$emit发布事件 - 传递值
import eventBus from '@u/eventBus'
eventBus.$emit('send',‘hello')
具体页面使用$on订阅事件 - 接收组件值
import eventBus from '@u/eventBus'
eventBus.$on('send', msg => {
console.log(msg) //控制台输出 hello
}
注意:$on先进行监听,一旦$emit发布事件后所有组件都可以$on监听到事件。所以传递参数的时候一定已经先进行了监听才能得到参数。比如在父组件中$emit事件放在mounted钩子函数中,等待子组件创建并$on开始监听事件后再去触发$emit发布事件。
$off()移除事件监听
import eventBus from '@u/eventBus'
eventBus.$off('send')
事件订阅功能$on是$eventBus对象完成的,与组件无关,如果用v-if销毁子组件的时候,会形成闭包,造成内存泄露,所有要在销毁组件的时候进行取消监听事件
具体形成原因点击查看
四丶$parent /$children与ref
$parent方法是在子组件中可以直接访问该组件的父实例或组件。
在父组件中定义一个切换显示页面执行中的显示方法。
switchLoadPage(msg) {
if(!this.loading && msg)
this.loadtext=msg;
this.loading = !this.loading;
},
在子组件中直接通过$parent去调用该方法
this.$parent.switchLoadPage();
$children方法是在父组件中可以直接访问子组件的实例,但是不保证子组件的顺序。
ref 被用来给DOM元素或子组件注册引用信息。引用信息会根据父组件的 $refs 对象进行注册。如果在普通的DOM元素上使用,引用信息就是元素; 如果用在子组件上,引用信息就是组件实例。
在父组件中自定义一个表格组件,给子组件加上 ref属性
<result ref="result" :config="dgConfig" ></result>
在父组件中就可以通过this.$refs.result去找到result子组件进行操作,比如把父组件的sdata直接放入子组件中
methods: {
info(){
this.$refs.result.sdata = this.sdata;
},
}
来源:https://blog.csdn.net/qq_45466204/article/details/122867453
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
零基础写python爬虫之抓取糗事百科代码分享
![](https://img.aspxhome.com/file/2023/6/134436_0s.png)
浅谈python和C语言混编的几种方式(推荐)
Go语言的Channel遍历方法详解
![](https://img.aspxhome.com/file/2023/4/101054_0s.png)
python加密打包程序详解
![](https://img.aspxhome.com/file/2023/9/103319_0s.png)
Python读取txt内容写入xls格式excel中的方法
如何创建支持FILESTREAM的数据库示例探讨
python实现KNN分类算法
![](https://img.aspxhome.com/file/2023/2/90702_0s.jpg)
截字符串 去除HTML标记
进一步了解Python中的XML 工具
python ImageDraw类实现几何图形的绘制与文字的绘制
![](https://img.aspxhome.com/file/2023/2/97032_0s.jpg)
python3模拟实现xshell远程执行linux命令的方法
![](https://img.aspxhome.com/file/2023/0/118390_0s.jpg)
python pyecharts库的用法大全
![](https://img.aspxhome.com/file/2023/3/76763_0s.png)
JS+DIV实现拖动效果
![](https://img.aspxhome.com/file/2023/4/137004_0s.gif)
将MySQL 5.0下的数据导入到MySQL 3.23中
mysql 导入导出数据库以及函数、存储过程的介绍
Windows Server 2003 服务器安全设置--防火墙篇
![](https://img.aspxhome.com/file/UploadPic/20107/22/2007520912952087-73s.jpg)
mysql5.6.19下子查询为什么无法使用索引
![](https://img.aspxhome.com/file/2023/1/72511_0s.png)
python装饰器原理源码示例分析
![](https://img.aspxhome.com/file/2023/6/133856_0s.png)
Python利用FlashText算法实现替换字符串
![](https://img.aspxhome.com/file/2023/3/82563_0s.png)
科讯CMS编辑器会自动更改代码
![](https://img.aspxhome.com/file/UploadPic/200812/12/kesion-editer-90s.jpg)