【vue】父子组件间通信----传函数
(一)子组件 调用 父组件 方法
方式一)
子组件中通过this.$parent.event来调用父组件的方法
父组件
<template>
<div>
<child></child>
</div>
</template>
<script>
import child from '~/components/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('父组件方法');
}
}
};
</script>
子组件
<template>
<div>
<button @click="childMethod()">点击</button>
</div>
</template>
<script>
export default {
methods: {
childMethod() {
this.$parent.fatherMethod();
}
}
};
</script>
方式二)
在子组件里用$emit向父组件触发一个事件,父组件监听这个事件
父组件
<template>
<div>
<child @fatherMethod="fatherMethod"></child>
</div>
</template>
<script>
import child from '~/components/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('父组件方法');
}
}
};
</script>
子组件
<template>
<div>
<button @click="childMethod()">点击</button>
</div>
</template>
<script>
export default {
methods: {
childMethod() {
this.$emit('fatherMethod');
}
}
};
</script>
方式三)
父组件把方法传入子组件中,在子组件里直接调用该方法
父组件
<template>
<div>
<child :fatherMethod="fatherMethod"></child>
</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('父组件方法');
}
}
};
</script>
子组件
<template>
<div>
<button @click="childMethod()">点击</button>
</div>
</template>
<script>
export default {
props: {
fatherMethod: {
type: Function,
default: null
}
},
methods: {
childMethod() {
if (this.fatherMethod) {
this.fatherMethod();
}
}
}
};
</script>
(二)父组件 调用 子组件 方法
方式一)
在子组件中加上ref,然后通过this.$refs.ref.method调用
父组件
<template>
<div @click="fatherMethod">
<child ref="child"></child>
</div>
</template>
<script>
import child from '~/components/child.vue';
export default {
components: {
child
},
methods: {
fatherMethod() {this.$refs.child.childMethods();
}
}
};
</script>
子组件
<template>
<div>{{test}}</div>
</template>
<script>
export default {
data() {
return {
test: '数据'
};
},
methods: {
childMethods() {
console.log("子组件方法");
}
}
};
</script>
【vue】父子组件间通信----传函数的更多相关文章
- vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单
参考: ElementUI多个子组件表单的校验管理:https://www.jianshu.com/p/541d8b18cf95 Vue 子组件调用父组件方法总结:https://juejin.im/ ...
- Vue的父子组件间通信及借助$emit和$on解除父子级通信的耦合度高的问题
1.父子级间通信,父类找子类非常容易,直接在子组件上加一个ref,父组件直接通过this.$refs操作子组件的数据和方法 父 这边子组件中 就完成了父 => 子组件通信 2. 子 =&g ...
- vue之父子组件间通信实例讲解(props、$ref、$emit)
组件间如何通信,也就成为了vue中重点知识了.这篇文章将会通过props.$ref和 $emit 这几个知识点,来讲解如何实现父子组件间通信. 组件是 vue.js 最强大的功能之一,而组件实例 ...
- Vue中组件间通信的方式
Vue中组件间通信的方式 Vue中组件间通信包括父子组件.兄弟组件.隔代组件之间通信. props $emit 这种组件通信的方式是我们运用的非常多的一种,props以单向数据流的形式可以很好的完成父 ...
- vue组件定义方式,vue父子组件间的传值
vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
- 【Vue】利用父子组件间通信实现一个场景
组件间通信是组件开发的,我们既希望组件的独立性,数据能互不干扰,又不可避免组件间会有联系和交互. 在vue中,父子组件的关系可以总结为props down,events up: 在vue2.0中废弃了 ...
- Vue 父子组件间的通信
前言 在 Vue 项目中父子组件的通信是非常常见的,最近做项目的时候发现对这方面的知识还不怎么熟练,在这边做一下笔记,系统学习一下吧. 1 父组件传值给子组件 1.1 传值写法 父组件传值给子组件,这 ...
- 聊聊Vue.js组件间通信的几种姿势
写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...
- vue非父子组件间通信
有时候非父子关系的组件也需要通信.在简单的场景下,使用一个空的Vue实例作为中央事件总线: 有时候非父子关系的组件也需要通信.在简单的场景下,使用一个空的 Vue 实例作为中央事件总线: var bu ...
随机推荐
- kohana附件上传
try { $upload = Uploader::factory('Picture', $_FILES['Filedata'])->execute();}catch (Exception $e ...
- weight(搜索对象的选取)
题目链接: 就是大概这么个东西 根据题意,我们可以清楚的知道:这个题我不会,这个题需要先将2n个数进行排序 这样每对于一个小的前(后)缀和总会在队列最前或队列最后 设这个数为k 那么判断总的Sum(n ...
- master-slave replication
redis save 备份 恢复 root@ubuntu:/etc/init.d# find / -name dump.rdb |xargs ls -alt redis-cli save cp /va ...
- sql数据库收缩
回收步骤: 1.查看日志文件大小[一般回收比较大的] --适用于RDS For SQL Server2012\2016 SELECT DB_NAME(database_id) AS [Database ...
- Celery多任务结构
视图结构 pro_cel ├── celery_task# celery相关文件夹 │ ├── celery.py # celery连接和配置相关文件,必须叫这个名字 │ └── tasks1.py ...
- day58—JavaScript面向对象(一)
转行学开发,代码100天——2018-05-13 “面向对象”对于学习过C++及JAVA的开发者来说并不陌生.有意思的是面向对象的思路可以用于面对或解决生活工作中的其他问题,简单地说就是“只关注功能, ...
- Delphi XE2 之 FireMonkey 入门(8) - TImage
TImage 主要成员: { 属性 } Bitmap : TBitmap; //图像 BitmapMargins : TBounds; ...
- JS手写状态管理的实现(转)
https://juejin.im/post/5c528411e51d456898361e43
- 测开之路一百零六:bootstrap布局
可以在html的head里面加一些说明 <meta http-equiv="X-UA-Compatible" content="IE=edge">& ...
- Linux 下在后台运行进程:nohup,setsid,& 以及 tmux
参考: Linux 技巧:让进程在后台可靠运行的几种方法 ssh 登录了远程服务器时,如果在前台运行耗时较长的任务, 当 ssh 掉线或关闭窗口时会导致命令停止运行. hup 与 nohup 当用户注 ...