vue2 组件通信
来源:互联网 发布:淘宝网体验中心在哪里 编辑:程序博客网 时间:2024/05/16 19:03
子组件传递数据给父组件
<body> <div id="box"> <parent></parent> </div> <template id='p'> <div> <h1>父组件~~~~~~{{ a }}</h1> <child1 :val='a' @change='getVal'></child1> </div> </template> <template id='c1'> <div> <h3>child1------{{ b }}~~~~{{ val }}<button @click='fn'>变</button></h3> </div> </template></body></html><script type="text/javascript"> var vm = new Vue({ el:'#box', data: {}, components: { parent: { template: '#p', data: function () { // body... return { a: '爸爸' } }, methods: { getVal: function (msg) { // body... // console.log(msg); this.a = msg; } }, // 逆向传值,vue中不允许,需要我们主动触发 components: { child1: { template: '#c1', data: function () { // body... return { b: '儿子' } }, props: ['val'], //希望得到的值 // 正向传值 vue是允许的 methods: { fn: function () { // body... // console.log(1); // 触发监听 $emit('自定义事件名称',值) // 谁触发,谁接受 this.$emit('change', this.b); } } } } } } });</script>
兄弟组件间通信
<body> <div id="box"> <child1></child1> <child2></child2> </div> <template id='c1'> <div> <h1>child1-->{{ msg }}<button @click='fn'>传值</button></h1> </div> </template> <template id='c2'> <div> <h3>child2-->{{ msg2 }}</h3> </div> </template></body></html><script type="text/javascript"> /* 同级组件间传值: 需要通过建立中转站(新的vue实例)来传值 同级间传值,需要主动触发 接收监听 实例上的$on方法来接收监听 */ var Hub = new Vue(); // 1) 中转站,其中不需要设置任何参数 var vm = new Vue({ el:'#box', data: {}, components: { child1: { template: '#c1', data: function () { return { msg: '我是哥哥' } }, methods: { fn: function () { // 2) 主动触发监听(中转站触发监听) Hub.$emit('change',this.msg); } } }, child2: { template: '#c2', data: function () { // body... return { msg2: '我是弟弟' } }, // 创建完成 created(){ // 3) 接收监听 $on('事件名称',function(val){}) val是传递过来的值 Hub.$on('change', function (val) { console.log(val);// this.msg2 = val; }); } } } });
阅读全文
0 0
- vue2 组件通信
- vue2.0 组件通信
- Vue2.0 组件通信
- Vue2.0组件之间通信
- vue2.0中的组件通信
- Vue2.0组件之间通信
- Vue2.0组件之间通信
- Vue2.0组件通信(非Vuex)
- Vue2.0子父组件通信
- vue2.0--组件通信(非vuex法)
- Vue2.0 父子组件之间通信
- vue2.0父子组件通信(慕课网)
- vue2.0——管理组件通信
- vue2.0父子组件间通信
- Vue2--非父子组件通信笔记
- Vue2.0组件通信(非Vuex)
- Vue2.0子父组件通信
- vue2.0--组件通信(非vuex法)
- Linux字符设备驱动程序编写基本流程
- C#中File类的文件操作方法详解
- 因为CircleImageView导致Binary XML file line #96: Error inflating class <unknown>
- 继承
- emWin使用资料收集(二) ----- 使用emWin和BmpCvt工具显示位图
- vue2 组件通信
- Laravel Carbon::diffForHumans 切换中文
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- hdu 4055
- wfi和wfe
- 原生JS轮播图
- 小米手机 miui 8.x开启开发者模式指导
- ubuntu 异常启动必须手动选择启动模式问题
- 生活虽然很多眼前的苟且,但要看到远方的田野