前面的话

  有时候需要父组件访问子组件,子组件访问父组件,或者是子组件访问根组件。 在组件实例中,Vue提供了相应的属性,包括$parent、$children、$refs和$root,这些属性都挂载在组件的this上。本文将详细介绍Vue组件实例间的直接访问

$parent

  $parent表示父组件的实例,该属性只读

  下面是一个简易实例

<div id="example">
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<input v-model="parentMsg">
<p>{{parentMsg}}</p>
<child-component></child-component>
</div>
</template>
<template id="child-component">
<div class="child">
<h3>我是子组件</h3>
<p>{{msg}}</p>
<button v-on:click="showData">显示父组件数据</button>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
parentMsg:'我是父组件的数据'
}
},
components:{
'child-component':{
template:'#child-component',
data(){
return{
msg:''
}
},
methods:{
showData(){
this.msg = this.$parent.parentMsg;
}
}
}
}
})
// 创建根实例
new Vue({
el: '#example'
})
</script>

$root

  $root表示当前组件树的根 Vue 实例。如果当前实例没有父实例,此实例将会是其自己。该属性只读

<div id="example">
<h3>我是根组件</h3>
<input v-model="rootMsg">
<p>{{rootMsg}}</p>
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<input v-model="parentMsg">
<p>{{parentMsg}}</p>
<child-component></child-component>
</div>
</template>
<template id="child-component">
<div class="child">
<h3>我是子组件</h3>
<p>
<button v-on:click="showRootData">显示根组件数据</button><span>{{rootMsg}}</span>
</p>
<p>
<button v-on:click="showParentData">显示父组件数据</button><span>{{parentMsg}}</span>
</p>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
parentMsg:'我是父组件的数据'
}
},
components:{
'child-component':{
template:'#child-component',
data(){
return{
parentMsg:'',
rootMsg:''
}
},
methods:{
showParentData(){
this.parentMsg = this.$parent.parentMsg;
},
showRootData(){
this.rootMsg = this.$root.rootMsg;
},
}
}
}
})
// 创建根实例
new Vue({
el: '#example',
data:{
rootMsg:'我是根组件数据'
}
})
</script>

$children

  $children表示当前实例的直接子组件。需要注意$children并不保证顺序,也不是响应式的。如果正在尝试使用$children来进行数据绑定,考虑使用一个数组配合v-for来生成子组件,并且使用Array作为真正的来源

<div id="example">
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<button @click="getData">获取子组件数据</button>
<br>
<div v-html="msg"></div>
<child-component1></child-component1>
<child-component2></child-component2>
</div>
</template>
<template id="child-component1">
<div class="child">
<h3>我是子组件1</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<template id="child-component2">
<div class="child">
<h3>我是子组件2</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
msg:'',
}
},
methods:{
getData(){
let html = '';
let children = this.$children;
for(var i = 0; i < children.length;i++){
html+= '<div>' + children[i].msg + '</div>';
}
this.msg = html;
}
},
components:{
'child-component1':{
template:'#child-component1',
data(){
return{
msg:'',
}
},
},
'child-component2':{
template:'#child-component2',
data(){
return{
msg:'',
}
},
},
}
})
// 创建根实例
new Vue({
el: '#example',
})
</script>

$refs

  组件个数较多时,难以记住各个组件的顺序和位置,通过序号访问子组件不是很方便

  在子组件上使用ref属性,可以给子组件指定一个索引ID:

<child-component1 ref="c1"></child-component1>
<child-component2 ref="c2"></child-component2>

  在父组件中,则通过$refs.索引ID访问子组件的实例

this.$refs.c1
this.$refs.c2
<div id="example">
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<div>
<button @click="getData1">获取子组件c1的数据</button>
<p>{{msg1}}</p>
</div>
<div>
<button @click="getData2">获取子组件c2的数据</button>
<p>{{msg2}}</p>
</div>
<child-component1 ref="c1"></child-component1>
<child-component2 ref="c2"></child-component2>
</div>
</template>
<template id="child-component1">
<div class="child">
<h3>我是子组件1</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<template id="child-component2">
<div class="child">
<h3>我是子组件2</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
msg1:'',
msg2:'',
}
},
methods:{
getData1(){
this.msg1 = this.$refs.c1.msg;
},
getData2(){
this.msg2 = this.$refs.c2.msg;
},
},
components:{
'child-component1':{
template:'#child-component1',
data(){
return{
msg:'',
}
},
},
'child-component2':{
template:'#child-component2',
data(){
return{
msg:'',
}
},
},
}
})
// 创建根实例
new Vue({
el: '#example',
})
</script>

总结

  虽然vue提供了以上方式对组件实例进行直接访问,但并不推荐这么做。这会导致组件间紧密耦合,且自身状态难以理解,所以尽量使用props自定义事件以及内容分发slot来传递数据

Vue组件实例间的直接访问的更多相关文章

  1. vue组件父子间通信之综合练习--假的聊天室

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. vue组件父子间通信02

    三.组件间通信($parent $refs) 父组件要想获取子组件的数据:①在调用子组件的时候,指定ref属性<child-component ref="mySon"> ...

  3. Vue 组件实例属性的使用

    前言 因为最近面试了二.三十个人,发现大部分都还是只是停留在 Vue 文档的教程.有部分连教程这部分的文档也没看全.所以稍微写一点,让新上手的 Vuer 多了解 Vue 文档的其他更需要关注的点. 因 ...

  4. Vue组件父子间通信01

    子组件传递数据 用户已经登录 父组件接收数据 并显示列表,未登录不显示列表 /* 有两个组件,分别是main-component,header-component.main-component是由he ...

  5. vue组件实例的生命周期

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. vue组件间通信六种方式(完整版)

    本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...

  7. Vue组件间通信方式到底有几种

    1. 前言 Vue的一个核心思想就是组件化.所谓组件化,就是把页面拆分成多个组件 (component),每个组件依赖的 CSS.JavaScript.模板.图片等资源放在一起开发和维护.组件是资源独 ...

  8. Vue组件间通信6种方式

    摘要: 总有一款合适的通信方式. 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的 ...

  9. 在被vue组件引用的 js 文件里获取组件实例this

    思路: 通过调用函数 把 组件实例this  传递 到 被应用的 js文件里 实例: 文件结构 在SendThis.vue 文件中引用 了modalConfig.js import modalConf ...

随机推荐

  1. VB6之断点续传

    闲来无事,研究了下HTTP的断点续传,用VB6写了小Demo. 关于HTTP-Range细节可参考: http://www.w3.org/Protocols/rfc2616/rfc2616.html ...

  2. RabbitMQ系列教程之六:远程过程调用(RPC)

    远程过程调用(Remote Proceddure call[RPC])(本实例都是使用的Net的客户端,使用C#编写)  在第二个教程中,我们学习了如何使用工作队列在多个工作实例之间分配耗时的任务.  ...

  3. FTP DOS 命令行

    1. 在cmd--> 输入ftp 2. 进入ftp输入提示命令行,此时输入open ftp服务器地址,比如我的是本机就: open 127.0.0.1 3. 根据提示输入用户名和密码, 提示登录 ...

  4. JavaScript一个猜数字游戏

    效果图: 代码: <body> <script type="text/javascript"> window.onload = newgame; //页面载 ...

  5. jsp注册页面的省份联动(网上copy别人的,然后自己弄了一下才知道怎么用)

    首先写一个js里面是所有的省份一些七七八八的东西,直接复制黏贴过去就好了. var addressInit = function(_cmbProvince, _cmbCity, _cmbArea, d ...

  6. wampserver2.5局域网IP访问配置

    wampserver2.5集成环境的安装和使用就不多说了,网上有很多教材.安装好后找到apache的配置文件httpd.conf.默认位置是: swap安装目录\wamp\bin\apache\apa ...

  7. Java工程师的终极书单

    本份Java工程师的终极书单只在专业的Java技术博客–天天编码上发布,没有授权任何网站与个人转载. 坚持阅读好书是学习Java技术的好方式.但是,市面上与Java技术相关的书籍可谓数不胜数,如何从这 ...

  8. hdu_1711: Number Sequence【KMP算法】

    题目链接 此次插播点笔记 hdu中点击蓝色的"Compilation Error"可以查看自己是为什么CE的 hdu中提交的话,语言选择G++可以使用<bits/stdc++ ...

  9. jmeter之BeanShell对两个变量断言对比

    在jmeter的中,断言没法对两个变量的进行对比后判断,只能使用Bean Shell断言来进行. 假设需求: 获取某类型用户uid个数与数据库查询结果是否相等 获取uid个数用http接口获取统计数据 ...

  10. (转)mq经验总结-转

    场景:学习mq相关的知识,发现这是一篇总结性很强的文章,转过来学习学习! 1 mq经验总结 首先了解什么是mq?mq的作用是什么? mq是通讯中间件.他的作用是省去开发人员开发通讯工具的时间,节省开发 ...