Vue组件间通信方式
来源:互联网 发布:优酷客户端for mac 编辑:程序博客网 时间:2024/06/05 05:24
本文描述Vue组件之间通信的几种方式:
一、emit
肤浅的认识,通过emit可以实现子组件向父组件之间传值,具体请看下面样式:
- 在父组件中做相应绑定
<div class="main-content">
<Evaluate @emitSend="emitReceive"></Evaluate>
</div>
<Evaluate @emitSend="emitReceive"></Evaluate>
</div>
其中‘emitSend’是后续emit向父组件传递数据时的key,'emitReceive'为父组件接收时的函数
- 子组件中使用emit传递数据
this.$emit('emitSend', {
result: 'EmitDemo'
})
result: 'EmitDemo'
})
- 父组件中接收数据
emitReceive: function (val) {
console.info(val)
}
console.info(val)
}
二、$refs
$refs通过给子组件做标记(xx),然后通过this.$refs.xx就可以访问子组件中的数据和函数了(数据可访问、可赋值)
- 父组件中为子组件做标记
<div class="main-content">
<Evaluate ref="refTest"></Evaluate>
</div>
<Evaluate ref="refTest"></Evaluate>
</div>
- 父组件中调用子组件中内容
在子组件中数据内容:
export default {
data () {
return {
total: 10
data () {
return {
total: 10
}
},
methods: {
// 测试父组件访问子组件功能
refTests: function () {
console.log('父组件访问子组件了!!')
}
}
}
},
methods: {
// 测试父组件访问子组件功能
refTests: function () {
console.log('父组件访问子组件了!!')
}
}
}
调用方式:
refDemo: function () {
console.info(this.$refs.refTest.total) // 获取子组件中值
this.$refs.refTest.total = 100 // 为子组件数据赋值
this.$refs.refTest.refTests() // 调用子组件中函数
}
console.info(this.$refs.refTest.total) // 获取子组件中值
this.$refs.refTest.total = 100 // 为子组件数据赋值
this.$refs.refTest.refTests() // 调用子组件中函数
}
三、$children和$parent
- 父组件访问子组件(注意子组件在父组件中的位置)(数据可访问、可赋值)
console.info(this.$children[3].total)
this.$children[3].total = 100
this.$children[3].refTests()
this.$children[3].total = 100
this.$children[3].refTests()
- 子组件访问父组件 (数据可访问、可赋值)
this.$parent.def_act = 'xxx'
console.info(this.$parent.def_act)
console.info(this.$parent.emitReceive(123123))
阅读全文
0 0
- Vue组件间通信方式
- Angularjs Controller 间通信方式、vue组件之间通信
- Vue组件间通信
- vue组件间通信
- vue组件间通信
- Vue组件间通信
- vue的组件间通信
- Vue组件间的通信
- Vue 组件间通信实例
- vue组件及组件间通信
- Vue 父子组件、组件间通信
- 使用bus实现Vue组件间通信
- 浅谈Vue组件间的通信
- Vue之父子兄弟组件间通信
- vue 父子组件通信
- Vue组件通信
- vue父子组件通信
- Vue父子组件通信
- jsp提交表单的方法
- hdu 2087 剪花布条(kmp)
- vue生命周期和钩子函数的理解
- OpenCV2编程手册笔记之 10.3处理视频帧
- spark中的几个概念File,Block,Split,Task,Partition,RDD以及节点数、Executor数、core数目的关系。
- Vue组件间通信方式
- 新ListView多条目加载 4个(重要)
- zendframework2配置Module
- 超级台阶
- azkaban源码解读
- It怎样从基层做起
- android字体和可免费商用的字体
- 添加nginx为系统服务(service nginx start/stop/restart)
- 异常抛出e.printStackTrace();工作原理