Vue2---父子组件之间的访问
阅读目录
Vue2--父子组件的访问
父组件访问子组件,子组件访问父组件,或者子组件访问根组件,Vue.js 都提供了相对应的API。
1. 父组件访问子组件,使用 $children 或 $refs
2. 子组件访问父组件;使用 $parent
如下代码定义了 父组件<parent-component>,父组件模板定义了2个子组件;在父组件中,通过 this.$children 可以访问子组件。
this.$children 是一个数组,它包含所有子组件的实列;如下代码:
<!DOCTYPE html>
<html>
<body>
<head>
<title>演示Vue</title>
</head>
<div id='app'>
<parent-component></parent-component>
</div>
<template id="child-component1">
<p>{{ msg }}</p>
</template>
<template id="child-component2">
<p>{{ msg }}</p>
</template>
</body>
<script src="./vue.js"></script>
<script type="text/javascript">
Vue.component('parent-component', {
template: '<div><child-component1></child-component1><child-component2></child-component2><button @click="showmsg">显示子组件的数据</button></div>',
components: {
'child-component1': {
template: '#child-component1',
data: function() {
return {
msg: '这是子组件1'
}
}
},
'child-component2': {
template: '#child-component2',
data: function() {
return {
msg: '这是子组件2'
}
}
}
},
methods: {
showmsg: function() {
for (var i = 0; i < this.$children.length; i++) {
alert(this.$children[i].msg);
}
}
}
});
new Vue({
el: '#app'
})
</script>
</html>
理解$refs
在子组件上使用 ref指令,可以给子组件指定一个索引ID,如下代码:
<child-component1 ref="A1"></child-component1><child-component2 ref="A2"></child-component2>
在父组件中,则通过$refs.索引ID访问子组件的实例:
showmsg: function() {
  alert(this.$refs.A1.msg);
  alert(this.$refs.A2.msg);
}
所有的代码如下:
<!DOCTYPE html>
<html>
<body>
<head>
<title>演示Vue</title>
</head>
<div id='app'>
<parent-component></parent-component>
</div>
<template id="child-component1">
<p>{{ msg }}</p>
</template>
<template id="child-component2">
<p>{{ msg }}</p>
</template>
</body>
<script src="./vue.js"></script>
<script type="text/javascript">
Vue.component('parent-component', {
template: '<div><child-component1 ref="A1"></child-component1><child-component2 ref="A2"></child-component2><button @click="showmsg">显示子组件的数据</button></div>',
components: {
'child-component1': {
template: '#child-component1',
data: function() {
return {
msg: '这是子组件1'
}
}
},
'child-component2': {
template: '#child-component2',
data: function() {
return {
msg: '这是子组件2'
}
}
}
},
methods: {
showmsg: function() {
alert(this.$refs.A1.msg);
alert(this.$refs.A2.msg);
}
}
});
new Vue({
el: '#app'
})
</script>
</html>
理解$parent
下面有一个子组件 child-component 和一个父组件 parent-component, 在子组件中,通过 this.$parent 可以访问到父组件的实例;如下代码:
<!DOCTYPE html>
<html>
<body>
<head>
<title>演示Vue</title>
</head>
<div id='app'>
<parent-component></parent-component>
</div>
<template id="child-component">
<div>
<p>111111</p>
<button @click="showmsg">显示父组件的实例</button>
</div>
</template>
</body>
<script src="./vue.js"></script>
<script type="text/javascript">
Vue.component('parent-component', {
template: '<div><child-component></child-component></div>',
components: {
'child-component': {
template: '#child-component',
methods: {
showmsg: function() {
alert(this.$parent.msg);
}
}
}
},
data: function() {
return {
msg: 'parent component msg'
}
}
});
new Vue({
el: '#app'
})
</script>
</html>
Vue2---父子组件之间的访问的更多相关文章
- Vue2.0父子组件之间和兄弟组件之间的数据交互
		熟悉了Vue.js的同级组件之间通信,写此文章,以便记录. Vue是一个轻量级的渐进式框架,对于它的一些特性和优点,请在官网上进行查看,不再赘述. 使用NPM及相关命令行工具初始化的Vue工程,目录结 ... 
- Vue2.0父子组件之间的双向数据绑定问题解决方案
		对于vue 1.0项目代码,如果把vue换成vue 2.0,那么之后项目代码就完全奔溃不能运行,vue 2.0在父子组件数据绑定的变化(不再支持双向绑定)颠覆了1.0的约定,很遗憾. 解决方案只有两种 ... 
- Vue.js 父子组件之间通信的方式
		Vue 父子组件之间的同学有一下几种方式: 1. props 2. $emit -- 组件封装用的比较多 3. .sync -- 语法糖 4. $attrs 和 $listeners -- 组件封装用 ... 
- Vue(基础四)_总结五种父子组件之间的通信方式
		一.前言 这篇文章主要总结了几种通信方式: 1.方式一:使用props: [ ]和$emit() (适用于单层通信) 2.方式二:$attrs和$listeners(适用于多层) 3.方式三:中央处 ... 
- 【Vue课堂】Vue.js 父子组件之间通信的十种方式
		这篇文章介绍了Vue.js 父子组件之间通信的十种方式,不管是初学者还是已经在用 Vue 的开发者都会有所收获.无可否认,现在无论大厂还是小厂都已经用上了 Vue.js 框架,简单易上手不说,教程详尽 ... 
- vue之父子组件之间的通信方式
		(一)props与$emit <!-这部分是一个关于父子组件之间参数传递的例子--> <!--父组件传递参数到子组件是props,子组件传递参数到父组件是用事件触发$emit--&g ... 
- 【转】vue父子组件之间的通信
		vue父子组件之间的通信 在vue组件通信中其中最常见通信方式就是父子组件之中的通性,而父子组件的设定方式在不同情况下又各有不同.最常见的就是父组件为控制组件子组件为视图组件.父组件传递数据给子组件使 ... 
- React 学习(六) ---- 父子组件之间的通信
		当有多个组件需要共享状态的时候,这就需要把状态放到这些组件共有的父组件中,相应地,这些组件就变成了子组件,从而涉及到父子组件之间的通信.父组件通过props 给子组件传递数据,子组件则是通过调用父组件 ... 
- vue中父子组件之间的传值、非父子组件之间的传值
		在Vue实例中每个组件之间都是相互独立的,都有自己的作用域,所以组件之间是不能直接获取数据.在项目开发中一个组件可能需要获取另一个组件的值,我们可以通过其他方法间接的获取.所以,获取的方法有以下几种: ... 
- vuejs组件交互 - 01 - 父子组件之间的数据交互
		父子组件之间的数据交互遵循: props down - 子组件通过props接受父组件的数据 events up - 父组件监听子组件$emit的事件来操作数据 示例 子组件的点击事件函数中$emit ... 
随机推荐
- (3)Microsoft office Word 2013版本操作入门_段落设定
			1.查看文件: 打开word查看左下角 会显示 word一共有多少页,当前第几页,共多少字等,如下图所示 2.word快速翻页: Ctrl+PageDown 向下翻页, Ctrl+PageUp 向上 ... 
- 精选20道Java代码笔试题
			1.运算符优先级问题,下面代码的结果是多少? public class Test { public static void main(String[] args) { int k = 0; int r ... 
- C#特性之数据类型
			这篇文章主要通过演示类在不同发展中的不通过定义方法,来向读者表述它们之间的区别和联系. 在C#1时代,我们喜欢这样定义类: public class Product { private string ... 
- Tomcat性能调优 通过ExpiresFilter设置资源缓存
			转自 http://www.cnblogs.com/daxin/p/3995287.html [简介] ExpiresFilter是Java servlet API 当中的一部分,它负责控制设置res ... 
- Chrome浏览器跨域
			配置新版Chrome浏览器跨域,需要创建用户数据文件夹,在其中保存浏览器的缓存.历史记录.收藏夹等数据. Windows系统Chrome跨域 1 下载Chrome 64位绿色版,解压缩,并在桌面创建快 ... 
- python自动化开发-7
			socket编程 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对 ... 
- Jenkins 开启用户注册机制及用户权限设置
			Jenkins 开启用户注册机制及用户权限设置 by:授客 QQ:1033553122 步骤 1. 系统管理-Configure Global Security 2. 设置 
- Python 获取被调用函数名称,所处模块,被调用代码行
			获取被调用函数名称,所处模块,被调用代码行 by:授客 QQ:1033553122 module2.py: #!/usr/bin/env python # -*- coding:utf-8 -*- _ ... 
- 修复cocos2dx的Label,WP8下不能换行的问题
			注:2014年12月23日有内存/性能优化更新,内容在下面分割线后 搞了几个小时,这个头疼的问题,我给出代码吧. 找到 libcocos2d/platform/winrt/CCFreeTypeFont ... 
- (后端)java回调机制
			转自强哥: 所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数.例如Win32下的窗口过程函数就是一个典型的回调函数. ... 
