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 ...
随机推荐
- 碧砚适合佳能328 4452 ICD520 4472 4450 硒鼓4700一体机墨盒4770
- java截屏简单例子
原文:http://www.open-open.com/code/view/1444211411979 java截屏 * 运行后将当前屏幕截取,并最大化显示. * 拖拽鼠标,选择自己需要的部分. * ...
- [机房合作]—SqlHelper我们又约了
一.是什么? SqlHelper是一个基于·NET Framework的数据库操作组件,组件中包括数据库操作方法. 二.为什么? 为什么要用SqlHelper类? 1.SqlHelper用 ...
- C#趣味程序---百鸡百钱
问题:公鸡一仅仅5元,母鸡一仅仅3元,小鸡三仅仅1元.问100元能够买多少仅仅鸡? using System; namespace ConsoleApplication1 { class Progra ...
- smartfoxserver扩展里面过滤聊天的不合法字符
http://blog.csdn.net/yc7369/article/details/35567105 近期做手游客户要求加上一个聊天功能.事实上嘛,个人认为这个聊天功能比較鸡肋,这部分差点儿已经有 ...
- Linux下怎么添加和查看PATH环境变量
linux下查看和添加PATH环境变量来自:http://apps.hi.baidu.com/share/detail/32942984 $PATH:决定了shell将到哪些目录中寻找命令或程序,PA ...
- 2016/4/5 Ajax ①用户名 密码 登陆 注册 ② 判断用户名是否已存在 ③点击按钮出现民族选项下拉菜单 ④DBDA类 加入Ajaxquery方法 数组变字符串 字符串拆分
①登陆 注册 查表匹配 0405Ajax.php ②判断用户名是否存在 <!DOCTYPE html> <html lang="en"> ...
- 2016/2/24 1,css有几种引入方式 2,div除了可以声明id来控制,还可以声明什么控制? 3,如何让2个div,并排显示。4,清除浮动 clear:left / right / both
1,css有几种引入方式 使用HTML标签的STYLE属性 将STYLE属性直接加在单个的HTML元素标签上,控制HTML标签的表现样式.这种引入CSS的方式是分散灵活方便,但缺乏整体性和规划性,不利 ...
- (转)SQL中使用or影响性能的解决办法
原文地址:https://www.cnblogs.com/xuxiaona/p/4962727.html 近期做了一个存储过程,执行时发现非常的慢,竟然需要6.7秒! 经排查,发现时间主要都耗在了其中 ...
- JSON: Circular Dependency Errors
If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directiona ...