Vuejs第十篇之vuejs父子组件通信


Posted in Javascript onSeptember 06, 2016

本篇文章是小编结合官方文档整理的一套更加细致,代码更多更全的教程,非常不错,比较适合新手阅读。

本篇资料来于官方文档:

http://cn.vuejs.org/guide/components.html#u7236_u5B50_u7EC4_u4EF6_u901A_u4FE1

父子组件通信

①访问子组件、父组件、根组件;

this.$parent 访问父组件

this.$children 访问子组件(是一个数组)

this.$root 根实例的后代访问根实例

示例代码:

<div id="app"> 
父组件: 
<input v-model="val"><br/> 
子组件: 
<test :test="val"></test> 
</div> 
<script> 
var vm = new Vue({ 
el: '#app', 
data: { 
val: 1 
}, 
components: { 
test: { 
props: ['test'], 
template: "<input @keyup='findParent' v-model='test'/>", 
methods: { 
findParent: function () { 
console.log(this.$parent); //访问根组件 
console.log(this.$parent.val); //访问根组件的val属性 
console.log(this.$parent.$children.indexOf(this)); //查看当前能否在其父组件的子组件中找到索引 
console.log(this.$parent === this.$root); //查看父组件和根组件是不是全等的(因为他的父组件就是根组件) 
} 
} 
} 
} 
}); 
</script>

当在子组件的输入框按键弹起时,显示内容依次为:

父组件、父组件的输入框的值(默认情况是1)、0(表示是父组件的children属性中的第一个元素)、true(由于父组件就是根组件,所以是全等的);

通过这样的方法,可以在组件树中进行互动。

②自定义事件:

首先,事件需要放置在events属性之中,而不是放置在methods属性中(新手很容易犯的错误),只能触发events属性中的事件,而methods属性中的事件是无法触发的。

其次,向上派发和向下广播有所区别:向上派发会触发自身同名事件,而向下广播不会;

第三,向上派发和向下广播默认只会触发直系(子或者父,不包括祖先和孙)的事件,除非事件返回值为true,才会继续在这一条线上继续。

第四,事件不能显式的通过 this.事件名 来调用它。

示例代码:

<div id="app"> 
父组件: 
<button @click="parentClick">点击向下传播broadcast</button> 
<br/> 
子组件1: 
<children1></children1> 
<br/> 
另一个子组件1: 
<another-children1></another-children1> 
</div> 
<script> 
var vm = new Vue({ 
el: '#app', 
data: { 
val: 1 
}, 
methods: { 
parentClick: function () { 
this.$broadcast("parentClick", "abc"); 
} 
}, 
events: { 
childrenClick: function () { 
console.log("childrenClick-Parent"); 
}, 
parentClick: function () { 
console.log("parentClick-Parent"); 
return true; 
} 
}, 
components: { 
children1: { //这个无返回值,不会继续派发 
props: ['test'], 
template: "<button>children1</button></br>子组件2:<children2></children2>", 
events: { 
childrenClick: function () { 
console.log("childrenClick-children1"); 
}, 
parentClick: function (msg) { 
console.log("parentClick-Children1"); 
console.log("message:" + msg); 
} 
}, 
components: { 
children2: { 
props: ['test'], 
template: "<button @click='findParent'>children-Click</button>", 
methods: { 
findParent: function () { 
this.$dispatch('childrenClick'); 
} 
}, 
events: { 
childrenClick: function () { 
console.log("childrenClick-children2"); 
}, 
parentClick: function (msg) { 
console.log("parentClick-Children2"); 
console.log("message:" + msg); 
} 
} 
} 
} 
}, 
anotherChildren1: { //这个是返回值为true,会继续向子组件的子组件派发 
props: ['test'], 
template: "<button>anotherChildren1</button></br>另一个子组件2:<another-children2></another-children2>", 
events: { 
childrenClick: function () { 
console.log("childrenClick-anotherChildren1"); 
return true; 
}, 
parentClick: function (msg) { 
console.log("parentClick-anotherChildren1"); 
console.log("message:" + msg); 
return true; 
} 
}, 
components: { 
anotherChildren2: { 
props: ['test'], 
template: "<button @click='findParent'>anotherChildren2-Click</button>", 
methods: { 
findParent: function () { 
this.$dispatch('childrenClick'); 
} 
}, 
events: { 
childrenClick: function () { 
console.log("childrenClick-anotherChildren2"); 
}, 
parentClick: function (msg) { 
console.log("parentClick-anotherChildren2"); 
console.log("message:" + msg); 
} 
} 
} 
} 
} 

} 
}); 
</script> 
}, 
parentClick: function () { 
console.log("parentClick-anotherChildren2"); 
} 
} 
} 
} 
} 

} 
}); 
</script>

说明:

【1】点击父组件的按钮,会向下广播,然后触发子组件1本身,另外一个子组件1,以及另一个子组件2;

【2】点击子组件2的按钮,会触发子组件2的事件和子组件1的事件,但不会触发父组件的按钮;

【3】点击另一个子组件2的按钮,会触发另一个子组件2的事件,另一个子组件1的事件和父组件的事件(因为另一个子组件1的事件的返回值为true);

③使用v-on绑定自定义事件:

【1】简单来说,子组件触发某个事件(events里的方法)时,父组件也会执行某个方法(父组件methods里的方法)。

【2】触发的绑定写在模板之中(即被替换的那个template模板中),可以多个子组件的事件绑定一个父组件的方法,或者不同子组件的事情绑定不同父组件的方法,但是不能同一个子组件事件绑定多个父组件的方法。

【3】子组件派发消息传递的参数,即使子组件的事件没有参数,也不影响将参数传递给父组件的方法(即父组件的方法可以接受到子组件方法获取的参数)

如示例:

<div id="app"> 
父组件: 
<button>点击向下传播broadcast</button> 
<br/> 
子组件1: 
<!--绑定写在这里,可以多个绑定同一个,或者不同绑定不同的,但不能一个绑定多个--> 
<children v-on:test="parent" @test2="another"></children> 
</div> 
<script> 
var vm = new Vue({ 
el: '#app', 
data: { 
val: 1 
}, 
methods: { 
parent: function (arg) { 
console.log(arg); 
console.log("the first method with test event"); 
}, 
another: function () { 
console.log("another method"); 
} 
}, 
components: { 
children: { //这个无返回值,不会继续派发 
props: ['test'], 
template: "<button @click='childClick'>children1</button></br><button @click='childClick2'>children1</button>", 
methods: { 
childClick: function () { 
this.$emit("test", 'the argument for dispatch'); 
}, 
childClick2: function () { 
this.$emit("test2"); 
} 
}, 
events: { 
test: function () { 
console.log("test"); 
}, 
test2: function () { 
console.log("test2"); 
} 
} 
} 
} 
}); 
</script>

④子组件索引

简单来说:就是可以直接从索引获取到子组件,然后就可以调用各个子组件的方法了。

添加索引方法是:在标签里添加v-ref:索引名

调用组件方法是:vm.$ref.索引名

也可以直接在父组件中使用this.$ref.索引名

这个时候,就可以获得组件了,然后通过组件可以调用他的方法,或者是使用其数据。

示例代码:

<div id="app"> 
父组件: 
<button @click="todo">触发子组件的事件</button> 
<br/> 
子组件1: 
<!--绑定写在这里,可以多个绑定同一个,或者不同绑定不同的,但不能一个绑定多个--> 
<children v-ref:child></children> 
</div> 
<script> 
var vm = new Vue({ 
el: '#app', 
methods: { 
todo: function () { 
this.$refs.child.fromParent(); //通过索引调用子组件的fromParent方法 
} 
}, 
components: { 
children: { //这个无返回值,不会继续派发 
props: ['test'], 
template: "<button>children1</button>", 
methods: { 
fromParent: function () { 
console.log("happened fromParent by ref"); 
} 
} 
} 
} 
}); 
</script>

以上所述是小编给大家介绍的Vuejs第十篇之vuejs父子组件通信,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
详解AngularJS的通信机制
Jun 18 Javascript
jQuery简单实现两级下拉菜单效果代码
Sep 15 Javascript
js获取图片宽高的方法
Nov 25 Javascript
EasyUi combotree 实现动态加载树节点
Apr 01 Javascript
基于vue实现多引擎搜索及关键字提示
Mar 16 Javascript
angularjs+bootstrap实现自定义分页的实例代码
Jun 19 Javascript
VueJS 集成 Medium Editor的示例代码 (自定义编辑器按钮)
Aug 24 Javascript
gulp教程_从入门到项目中快速上手使用方法
Sep 14 Javascript
JS实现websocket长轮询实时消息提示的效果
Oct 10 Javascript
jQuery-ui插件sortable实现自由拖动排序
Dec 01 jQuery
vue实现文字横向无缝走马灯组件效果的实例代码
Apr 09 Javascript
Jquery高级应用Deferred对象原理及使用实例
May 28 jQuery
Vue.js每天必学之方法与事件处理器
Sep 06 #Javascript
基于JavaScript实现购物网站商品放大镜效果
Sep 06 #Javascript
AngularGauge 属性解析详解
Sep 06 #Javascript
AngularJS 面试题集锦
Sep 06 #Javascript
BootStrap glyphicon图标无法显示的解决方法
Sep 06 #Javascript
JS原型链 详解及示例代码
Sep 06 #Javascript
JavaScript中push(),join() 函数 实例详解
Sep 06 #Javascript
You might like
颠覆常识!无色透明的咖啡诞生了(中日双语)
2021/03/03 咖啡文化
浅谈PHP 闭包特性在实际应用中的问题
2009/10/30 PHP
php下正则来匹配dede模板标签的代码
2010/08/21 PHP
Javascript基础教程之定义和调用函数
2015/01/18 Javascript
jQuery内容过滤选择器用法分析
2015/02/10 Javascript
学习掌握JavaScript中this的使用技巧
2016/08/29 Javascript
angular学习之从零搭建一个angular4.0项目
2017/07/10 Javascript
详解组件库的webpack构建速度优化
2018/06/18 Javascript
js中call()和apply()改变指针问题的讲解
2019/01/17 Javascript
vuex actions异步修改状态的实例详解
2019/11/06 Javascript
写了个监控nginx进程的Python脚本
2012/05/10 Python
python实现的简单抽奖系统实例
2015/05/22 Python
Python Requests安装与简单运用
2016/04/07 Python
Python使用logging结合decorator模式实现优化日志输出的方法
2016/04/16 Python
Python中在for循环中嵌套使用if和else语句的技巧
2016/06/20 Python
python 把数据 json格式输出的实例代码
2016/10/31 Python
python机器学习理论与实战(四)逻辑回归
2018/01/19 Python
Python进阶之尾递归的用法实例
2018/01/31 Python
python按行读取文件,去掉每行的换行符\n的实例
2018/04/19 Python
python将字符串以utf-8格式保存在txt文件中的方法
2018/10/30 Python
pycharm 将python文件打包为exe格式的方法
2019/01/16 Python
我喜欢你 抖音表白程序python版
2019/04/07 Python
python爬虫基础教程:requests库(二)代码实例
2019/04/09 Python
pyQT5 实现窗体之间传值的示例
2019/06/20 Python
Python开发入门——迭代的基本使用
2020/09/03 Python
pip已经安装好第三方库但pycharm中import时还是标红的解决方案
2020/10/09 Python
英国泰坦旅游网站:全球陪同游览,邮轮和铁路旅行
2016/11/29 全球购物
英国最大的汽车配件在线商店:Euro Car Parts
2019/09/30 全球购物
Servlet方面面试题
2016/09/28 面试题
初中生庆国庆演讲稿范文2014
2014/09/25 职场文书
公路局群众路线教育实践活动第一阶段工作汇报
2014/10/25 职场文书
老人再婚离婚协议书范本
2014/10/27 职场文书
北京故宫的导游词
2015/01/31 职场文书
中标通知书格式
2015/04/17 职场文书
吴仁宝观后感
2015/06/09 职场文书
《英雄联盟》2022日蚀、月蚀皮肤演示 黑潮亚索曝光
2022/04/13 其他游戏