Vue父子组件传值之——访问根组件$root、$parent、$children和$refs
Vue组件传值除了prop和$emit,我们还可以直接获取组件对象:
根组件: $root // 单一对象
表示当前组件树的根 Vue 实例,即new Vue({...根组件内容})。
如果当前实例没有父实例,此实例将会是其自己。
Vue子组件可以通过$root属性访问父组件实例的属性和方法
父组件:$parent // 单一对象
$parent表示父组件的实例,该属性只读
子组件:$children // 数组
$children表示当前实例的直接子组件。需要注意$children并不保证顺序,也不是响应式的。如果正在尝试使用$children来进行数据绑定,考虑使用一个数组配合v-for来生成子组件,并且使用Array作为真正的来源
其他获取组件方法: $refs
$refs
栗子:
组件顺序:new Vue > app.vue > getComponentsVal.vue > (child-one.vue + child-tow.vue + <h2>three</h2>)
1分析配图:

2代码实现:
首先我们列出根组件和父组件:
根组件:(可以看到,这里引入了组件App)
new Vue({
components: { App },
template: '<App/>',
data: {msg: '#app'} // #app
}).$mount('#app20190124')
父组件:(这里只列出了msg内容,它就是上图中的App.vue组件块;并且引入了getComponentsVal.vue组件)
data () {
return {
msg: 'App' // App
}
}
然后我们着重分析当前测试本身和它的子组件:
当前测试的组件——template:当前组件”getComponentsVal.vue”包含了两个子组件和一个h2标签
<template>
<div>
<h2>我是"App"的子组件,我的根组件Vue#app20190124</h2>
<child-one ref="one"/>
<child-two ref="two"/>
<h2 ref="three">three</h2>
</div>
</template>
当前测试的组件——的两个子组件(这里没有import,而是直接写在components里):
components: {
childOne: {
name: 'child-one',
template: '<h2>{{msg}}</h2>',
data () {
return {
msg: 'child-one'
}
},
mounted () {
console.log(this.$parent.msg)
}
},
childTwo: {
name: 'child-two',
template: '<h2>{{msg}}</h2>',
data () {
return {
msg: 'child-two'
}
},
mounted () {
console.log(this.$parent.msg)
}
}
},
当前测试的组件——的测试结果:
mounted () {
console.group('根组件、父组件-------------')
console.log(this.$root) // 根实例对象: {Vue#app}
console.log(this.$root.msg) // #app
console.log(this.$parent) // 父组件实例对象: {Vue父}
console.log(this.$parent.msg) // App
console.groupEnd()
console.group('子组件--------------------')
console.log(this.$children) // 所有子级数组: [VueComponent, VueComponent]
console.log(this.$children[0].msg) // 获取第一个子级msg: child-one
console.log(this.$children[1].msg) // 获取第二个子级msg: child-two
console.groupEnd()
console.group('$refs--------------------')
console.log(this.$refs) // 包含所有refs的对象:{one: VueComponent, two: VueComponent, three: h2}
console.log(this.$refs.one) // 子级组件: child-one
console.log(this.$refs.two) // 子级组件: child-two
console.log(this.$refs.three) // dom元素: <h2>three</h2>
console.groupEnd()
}
3执行结果图:

4当前测试的组件——的全部代码:
<template>
<div>
<h2>我是"App"的子组件,我的根组件Vue#app20190124</h2>
<child-one ref="one"/>
<child-two ref="two"/>
<h2 ref="three">three</h2>
</div>
</template> <script>
export default {
name: 'getComponentsVal',
data () {
return {
msg: 'getComponentsVal'
}
},
components: {
childOne: {
name: 'child-one',
template: '<h2>{{msg}}</h2>',
data () {
return {
msg: 'child-one'
}
},
mounted () {
console.log(this.$parent.msg)
}
},
childTwo: {
name: 'child-two',
template: '<h2>{{msg}}</h2>',
data () {
return {
msg: 'child-two'
}
},
mounted () {
console.log(this.$parent.msg)
}
}
},
mounted () {
console.group('根组件、父组件-------------')
console.log(this.$root) // 根实例对象: {Vue#app}
console.log(this.$root.msg) // #app
console.log(this.$parent) // 父组件实例对象: {Vue父}
console.log(this.$parent.msg) // App
console.groupEnd()
console.group('子组件--------------------')
console.log(this.$children) // 所有子级数组: [VueComponent, VueComponent]
console.log(this.$children[0].msg) // 获取第一个子级msg: child-one
console.log(this.$children[1].msg) // 获取第二个子级msg: child-two
console.groupEnd()
console.group('$refs--------------------')
console.log(this.$refs) // 包含所有refs的对象:{one: VueComponent, two: VueComponent, three: h2}
console.log(this.$refs.one) // 子级组件: child-one
console.log(this.$refs.two) // 子级组件: child-two
console.log(this.$refs.three) // dom元素: <h2>three</h2>
console.groupEnd()
}
}
</script>
Vue父子组件传值之——访问根组件$root、$parent、$children和$refs的更多相关文章
- vue父子组件传值加例子
例子:http://element-cn.eleme.io/#/zh-CN/component/form 上进行改的 父传子:用prop:子组件能够改变父组件的值,是共享的,和父操作是 ...
- 关于Vue父子组件传值(复杂数据类型的值)的细节点
vue 父子组件传值是很常见的,多数情况下都是父传递给子的值是基础数据类型,如string,number,boolean, 当父组件值被修改时,子组件能够实时的作出改变. 如果父子传值的类型是复杂数据 ...
- 一个故事讲懂vue父子组件传值
作者:李佳明同学链接:https://www.jianshu.com/p/2272b6ca0f0c 一个故事讲懂vue父子组件传值 讲故事前先讲代码 父组件向子组件传值 父组件数据传递给子组件可以通过 ...
- Vue 父子组件传值 props
1.Vue 的渲染周期: vue 如何实现响应式追踪. 父子组件通信有很多方式,今天单独聊下props 的方式.我们通过查找官方文档很容发现,props传值有静态和动态两种传值方式. 当然props ...
- vue 父子组件传值以及方法调用,平行组件之间传值以及方法调用大全
vue项目经常需要组件间的传值以及方法调用,具体场景就不说了,都知道.基本上所有的传值都可以用vuex状态管理来实现,只要在组件内监听vuex就好. vue常用的传值方式以及方法有: 1. 父值传子( ...
- vue 父子组件传值,兄弟组件传值
父子组件中的传值 父向子 v-bind props <!-- 组件使用v-bind传值 --> <router :msg="msg"></rou ...
- Vue父子组件传值以及父调子方法、子调父方法
稍微总结了一下Vue中父子间传值以及相互调方法的问题,非常基础.希望可以帮到你!先来个最常用的,直接上代码: 1.父传值给子组件 父组件: <template> <div> & ...
- VUE父子组件传值问题
一.父组件向子组件传递数据 组件实例的作用域是孤立的.这意味着不能(也不应该)在子组件的模板内直接引用父组件的数据.要让子组件使用父组件的数据,我们需要通过子组件的props选项. 1.静态props ...
- vue父子组件传值
1.父组件向子组件传值 例如app.vue是父组件,v-header.vue是子组件,实现app向v-header传值父组件需要自定义自己的title值, 子组件v-header内容 <temp ...
随机推荐
- css三大布局
标准流: 从左到右,从上到下块级元素独占一行,行内元素碰到父盒子边缘换行 浮动: 特点 1 元素浮动之后不占据原来的位置(脱标),变成立体,下面可以有东西,只影响下面的 2 浮动的盒子在一行上显示 3 ...
- R-Tree空间索引算法的研究历程和最新进展分析
转自原文 R-Tree空间索引算法的研究历程和最新进展分析,2008 摘要:本文介绍了空间索引的概念.R-Tree数据结构和R-Tree空间索引的算法描述,并从R-Tree索引技术的优缺点对R-Tre ...
- Java处理XSS漏洞的工具类代码
原文:http://www.open-open.com/code/view/1455809388308 public class AntiXSS { /** * 滤除content中的危险 HTML ...
- Import Items – Validation Multiple Languages Description
ð 提交标准请求创建和更新物料,因语言环境与处理次序方式等因素,造成物料中英(更多语言)描述和长描述混乱刷新. 症状: >>> Submit Standard Open Inter ...
- POJ2573 Bridge 经典的过桥问题
曾经遇到过类似的.纪念一下!这题同一时候也是 ZOJ1877.经典的过桥问题 是有个博客解说的非常好的 戳这里 挺久曾经.遇到过一个基本一样的,那个题目仅仅要求求出 最短时间就可以,如今还有过桥的过 ...
- 2016/3/31 拾遗 php字符串中 转义字符 “ ’‘ ” ’ “” ‘ " \’ ' ' \‘ " " \" '' \ " " 使用
<?php echo $str_string1='甲问:"你在哪里学的PHP?"'; echo "<br />"; echo $str_str ...
- 2016/3/17 Mysq select 数学函数 字符串函数 时间函数 系统信息函数 加密函数
一,数学函数主要用于处理数字,包括整型.浮点数等. ABS(X) 返回x的绝对值 SELECT ABS(-1)--返回1 CEll(X),CEILING(x) 返回大于或等于x的最小整数 SELEC ...
- JDBC 详解
工作原理流程:装载驱动程序---->获得数据库连接---->使用Statement或PreparedStatement执行SQL语句----> 返回执行的结果---->关闭相关 ...
- hdu4908 & BestCoder Round #3 BestCoder Sequence(组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 BestCoder Sequence Time Limit: 2000/1000 MS (Jav ...
- leetcode 656. Coin Path
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...