前面的话

  有时候需要父组件访问子组件,子组件访问父组件,或者是子组件访问根组件。 在组件实例中,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. 图论基础之Dijkstra算法的初探

         图论,顾名思义就是有图有论.        图:由点"Vertex"和边"Edge "组成,且图分为有向图和无向图(本文讨论有向图),之前做毕业设计的 ...

  2. Java IO在实际项目开发中应用

    IO是java绕不过去的槛,在开发中io无处不在, 正如同 世界上本没有路,java io写多了,也就知道了大体是什么意思,在读完thinking in java 感觉就更清晰了,结合具体的业务场景, ...

  3. Broker模块划分

    本篇在上一篇<消息中间件架构讨论>的基础上分析Broker的模块划分. 上图是之前讨论确定的系统架构(后续内容会按照这个架构来叙述),几点基础: Broker采用主从结构 Broker负责 ...

  4. Hive实战之Youtube数据集

    1 数据来源 本次实战的数据来自于"YouTube视频统计与社交网络"的数据集,是西蒙弗雷泽大学计算机学院在2008年所爬取的数据 数据集地址 1. 1 Youtube视频表格式如 ...

  5. IDEA的热部署插件jrebel6.4.3离线安装版配置与破解

    JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...

  6. rsync安装及其配置

    服务端配置安装 服务器 第一步: 下载rsync 安装包(在线安装或者线下安装)         wget https://download.samba.org/pub/rsync/rsync-3.1 ...

  7. 虚拟机Linux系统下配置网络

    虚拟机上安装Redhat9.0后是没有网络的,而本来的Windows系统是可以上网的,此时想在Redhat上网就需要在Linux系统上配置网络,以下是笔者自己配置的一点心得. 1.电脑本机系统打开网络 ...

  8. xdu_1009: Josephus环的复仇(线段树)

    题目链接 题意不难理解,解法具体看代码及注释吧.. #include<bits/stdc++.h> using namespace std; typedef long long LL; ; ...

  9. zTree学习实例

    今天做完一个zTree的实例,供有需要的学习! 效果图如下:

  10. (转)Linux系统安装时分区的选择

    场景:对于Linux系统的分区总是迷迷茫茫的,还是实践少,基础不牢. 以前初识Linux时,对Linux系统安装时分区的选择,一点都不了解,导致几次没法进行下一步安装,因此就静下心来,专门拿出时间研究 ...