总结vue中父向子,子向父以及兄弟之间通信的几种方式
子向父方式1:通过props,如例子中子组件test1.vue向父组件App.vue传值
App.vue代码
<template>
<div id="app">
<test1 :parfn="parfn"></test1>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
export default {
name: 'App',
data () {
return {
msg: 'parent'
}
},
components: {test1},
methods: {
parfn: function (a) {
alert(a)
}
}
}
</script>
test1.vue代码
<template>
<div>i am test1</div>
</template> <script>
export default {
data () {
return {
msg: 'test1'
}
},
props: {
parfn: {
type: Function
}
},
created: function () {
this.parfn(this.msg)
}
}
</script>
效果图

子向父方式2:通过$parent
App.vue代码:
<template>
<div id="app">
<test1></test1>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
export default {
name: 'App',
data () {
return {
msg: 'parent'
}
},
components: {test1},
methods: {
parfn: function (a) {
alert(a)
}
}
}
</script>
test1.vue代码:
<template>
<div>i am test1</div>
</template> <script>
export default {
data () {
return {
msg: 'test1'
}
},
created: function () {
this.$parent.parfn(this.msg)
}
}
</script>
效果图:

子向父方式3:通过emit
App.vue代码
<template>
<div id="app">
<test1 @myfn="parfn"></test1>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
export default {
name: 'App',
data () {
return {
msg: 'parent'
}
},
components: {test1},
methods: {
parfn: function (a) {
alert(a)
}
}
}
</script>
test1.vue代码:
<template>
<div>i am test1</div>
</template> <script>
export default {
data () {
return {
msg: 'test1'
}
},
mounted: function () {
this.$emit('myfn', this.msg)
}
}
</script>
效果图:

父向子传值方式1:通过props
App.vue代码:
<template>
<div id="app">
<test1 :msg="msg"></test1>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
export default {
name: 'App',
data () {
return {
msg: 'parent'
}
},
components: {test1}
}
</script>
test1.vue代码:
<template>
<div>i am test1</div>
</template> <script>
export default {
props: ['msg'],
created: function () {
alert(this.msg)
}
}
</script>
效果图:

父向子方式2:通过$children
App.vue代码:
<template>
<div id="app">
<test1></test1>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
export default {
name: 'App',
data () {
return {
msg: 'parent'
}
},
mounted: function () {
this.$children[].childfn(this.msg)
},
components: {test1}
}
test1.vue代码
<template>
<div>i am test1</div>
</template> <script>
export default {
methods: {
childfn: function (a) {
alert(a)
}
}
}
</script>
效果图:

父向子方式3:通过ref
App.vue代码:
<template>
<div id="app">
<test1 ref="mychild"></test1>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
export default {
name: 'App',
data () {
return {
msg: 'parent'
}
},
mounted: function () {
this.$refs.mychild.childfn(this.msg)
},
components: {test1}
}
</script>
test1.vue代码:
<template>
<div>i am test1</div>
</template> <script>
export default {
methods: {
childfn: function (a) {
alert(a)
}
}
}
</script>
效果图:

兄弟间传值方式1:通过事件车,例如App.vue组件中两个兄弟组件test1.vue传值给test2.vue
步骤1:在外部如assets下创建bus.js
// bus.js
import Vue from 'vue'
export default new Vue()
步骤2:组件中引入相互传值的兄弟组件,如App.vue中test1组件传值给test2组件
<!--App.vue-->
<template>
<div id="app">
<test1></test1>
<test2></test2>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
import test2 from '@/components/test2.vue'
export default {
name: 'App',
components: {test1, test2}
}
</script>
步骤3:test1组件中引入bus,通过$emit发送事件
<template>
<div>
test1
<button @click="send">点击发送test1数据给test2</button>
</div>
</template> <script>
import bus from '@/assets/bus'
export default {
data () {
return {
msg: 'test1数据111'
}
},
methods: {
send: function () {
bus.$emit('myfn', this.msg)
}
}
}
</script>
步骤4:test2组件中引入bus,通过$on接收事件
<template>
<div>
i am test2,接收过来的数据为:{{msg}}
</div>
</template> <script>
import bus from '@/assets/bus'
export default {
data () {
return {
msg: ''
}
},
created: function () {
bus.$on('myfn', msg => {
this.msg = msg
})
}
}
</script>
效果图:

兄弟间传值方式2:子组件传数据给父,父再传给另一个兄弟组件,例如App.vue组件中两个兄弟组件test1.vue传值给test2.vue
步骤1:test1组件中通过$emit传递数据给父组件App.vue
<template>
<div>
test1
<button @click="send">点击发送test1数据给test2</button>
</div>
</template> <script>
export default {
data () {
return {
msg: 'test1数据111'
}
},
methods: {
send: function () {
this.$emit('myfn', this.msg)
}
}
}
</script>
步骤2:父组件App.vue中,通过v-bind绑定个属性传递给另一个子组件test2.vue
<!--App.vue-->
<template>
<div id="app">
<test1 @myfn="getmsg"></test1>
<test2 :msg="msg"></test2>
</div>
</template> <script>
import test1 from '@/components/test1.vue'
import test2 from '@/components/test2.vue'
export default {
name: 'App',
data () {
return {
msg: ''
}
},
methods: {
getmsg: function (msg) {
this.msg = msg
}
},
components: {test1, test2}
}
</script>
步骤3:test2.vue组件通过props接收父组件传递过来的参数
<template>
<div>
i am test2,接收过来的数据为:{{msg}}
</div>
</template> <script>
export default {
props: ['msg']
}
</script>
效果图:

总结vue中父向子,子向父以及兄弟之间通信的几种方式的更多相关文章
- Vue组件之间通信的三种方式
最近在看梁颠编著的<Vue.js实战>一书,感觉颇有收获,特此记录一些比价实用的技巧. 组件是MVVM框架的核心设计思想,将各功能点组件化更利于我们在项目中复用,这类似于我们服务端面向对象 ...
- vue组件之间通信的8种方式
对于vue来说,组件之间的消息传递是非常重要的,下面是我对组件之间消息传递的常用方式的总结. props和$emit(常用) $attrs和$listeners 中央事件总线(非父子组件间通信) v- ...
- Vue中利用$emit实现子组件向父组件通信
Vue中利用$emit实现子组件向父组件通信 父组件 <template> <div> <p>我是父组件</p> <child :isShow=& ...
- vue组件通信的几种方式
最近用vue开发项目,记录一下vue组件间通信几种方式 第一种,父子组件通信 一.父组件向子组件传值 1.创建子组件,在src/components/文件夹下新建一个Child.vue 2.Child ...
- vue中echarts 在element-ui的tab 切换时 width 为100px 时的解决方式
最近在项目中遇到了这种情况,需要在tab控件上渲染多个echart图标,然后切换查看时,发现图表的宽度不正确 原因:在页面进行加载时,隐藏的图表找不到对应的div大小,所以默认给了一个大小.所以要做的 ...
- (尚031)Vue_案例_自定义事件(组件间通信第2种方式:vue自定义事件)
自定义事件: 我们知道,父组件使用prop传递数据的子组件,但子组件怎么跟父组件通信呢? 这个时候Vue的自定义事件系统就派得上用场了. 自定义事件知道两件事: (1).绑定 (2).触发 注意:$o ...
- Vue自定义组件以及组件通信的几种方式
本帖子来源:小贤笔记 功能 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它 ...
- [转] React 中组件间通信的几种方式
在使用 React 的过程中,不可避免的需要组件间进行消息传递(通信),组件间通信大体有下面几种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面依次说下这几种通 ...
- React 中组件间通信的几种方式
在使用 React 的过程中,不可避免的需要组件间进行消息传递(通信),组件间通信大体有下面几种情况: 父组件向子组件通信 子组件向父组件通信 非嵌套组件间通信 跨级组件之间通信 1.父组件向子组件通 ...
随机推荐
- Win10升级惹的祸,Oracle服务全没有了,怎么解决?
最近Win10自动升级,升级后各种服务全部没了,心已经凉了一半... 百度\Google了一大堆,无非就几种:卸载重装,还原到上一版Win10,甚至重装系统,对于正式使用的库来说,够崩溃的了.. 搜寻 ...
- oracle 存储过程(分析理解)
我的理解 比较简单(仅供参考) 存储过程就相当于java里面的方法 简单讲就是一串代码能够实现某个特定的功能,想要使用这个方法直接调用方法名就能够使用他的功能,这就是方法 oracle 存储过程也 ...
- SKU : Stock Keeping Unit
Stock Keeping Unit is a number assigned to a product by a retail store to identify the price, produ ...
- English trip V2 - 5 Technology Teacher:Taylor Key:adjective + preposition
In this lesson you will learn to talk about technology and innovation. 课上内容(Lesson) What is your fav ...
- 交换机telnet配置
新开箱交换机开机配置Telnet需要三个步骤: 1.开启telnet是能:系统视图模式下输入命令: telnet server enable #开启telnet功能# 2.Telnet创建账号:aaa ...
- 关于git的诞生
看了某某大佬的Git教程,简单易懂的同时,也让我对Git有了进一步的了解.搞半天,我们顶礼膜拜的分布式版本控制系统Git,是在这样的情况下产生的. linus在创建开源的linux系统的时候,并不是他 ...
- 关于Jmeter的简单认识
Jmeter初识 Apache Jmeter概述 是100%的纯java桌面应用,用于压力测试与功能测试,它开始被设计用于web应用测试,后来扩展到其他测试领域,通过线程租来驱动多个线程运行测试脚本, ...
- Django全文检索(django-haystack+whoosh+jieba)
前言: 全文检索就是针对所有内容进行动态匹配搜索的概念,针对特定的关键词建立索引并精确匹配达到性能优化的目的 class Whoose_seach(object): analyzer = Chines ...
- VNPY - windows 安装踩坑记录
twisted requires PyHamcrest>=, which is not ins grin requires argparse>=1.1, which is not inst ...
- CentOS7系统上的LAPACK源码安装
参考链接:linux下安装blas和lapack BLAS 和 LAPACK 这两个数学库是很多 Linux 科学计算软件需要调用的,所以经常会用到. LAPACK,其名为Linear Algebra ...