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 ...
随机推荐
- POJ 3230 【DP】
题意: 某货旅行,在n个城市呆m天. 给出从第i个城市到第j个城市的路费,或者留在某个城市的生活费. 给出在第i天在第j个城市的收益. 可以在城市之间任意穿梭逗留没有其他特殊要求. 求收益最大是多少. ...
- Google的Guava类库简介(转)
说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...
- SpringMVC get请求中文乱码
针对GET请求的编码问题,则需要改tomcat的server.xml配置文件,如下: 原 <Connector connectionTimeout="20000" port= ...
- [转]JS 引擎的执行机制
转: https://www.cnblogs.com/wancheng7/p/8321418.html ------------------------------------------------ ...
- SecureCRT5 中文乱码
SecureCRT5 中文乱码 secureCRT7已经不用这样设置了: 学习了:http://www.iitshare.com/securecrt-chinese-garbled-solution. ...
- [RxJS] Implement RxJS `switchMap` by Canceling Inner Subscriptions as Values are Passed Through
switchMap is mergeMap that checks for an "inner" subscription. If the "inner" su ...
- [Tools] Scroll, Zoom, and Highlight code in a mdx-deck slide presentation with Code Surfer <🏄/>
If you have a presentation coming up or you just need to present some documentation, then the Code S ...
- Android开发艺术-第二章 IPC 机制
2.1 Android IPC 简单介绍 IPC 意为进程间通信或者跨进程通信,线程是 CPU 调度的最小单元,是一种有限的系统资源. 进程一般指一个执行单元.不论什么操作系统都须要对应的 IPC 机 ...
- asp.net mvc 抓取京东商城分类
555 asp.net mvc 抓取京东商城分类 URL:http://www.jd.com/allSort.aspx 效果: //后台代码 public ActionResult Get ...
- 【CODEFORCES】 C. Dreamoon and Strings
C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...