Vue组件实例间的直接访问
前面的话
有时候需要父组件访问子组件,子组件访问父组件,或者是子组件访问根组件。 在组件实例中,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组件实例间的直接访问的更多相关文章
- vue组件父子间通信之综合练习--假的聊天室
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- vue组件父子间通信02
三.组件间通信($parent $refs) 父组件要想获取子组件的数据:①在调用子组件的时候,指定ref属性<child-component ref="mySon"> ...
- Vue 组件实例属性的使用
前言 因为最近面试了二.三十个人,发现大部分都还是只是停留在 Vue 文档的教程.有部分连教程这部分的文档也没看全.所以稍微写一点,让新上手的 Vuer 多了解 Vue 文档的其他更需要关注的点. 因 ...
- Vue组件父子间通信01
子组件传递数据 用户已经登录 父组件接收数据 并显示列表,未登录不显示列表 /* 有两个组件,分别是main-component,header-component.main-component是由he ...
- vue组件实例的生命周期
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue组件间通信六种方式(完整版)
本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...
- Vue组件间通信方式到底有几种
1. 前言 Vue的一个核心思想就是组件化.所谓组件化,就是把页面拆分成多个组件 (component),每个组件依赖的 CSS.JavaScript.模板.图片等资源放在一起开发和维护.组件是资源独 ...
- Vue组件间通信6种方式
摘要: 总有一款合适的通信方式. 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的 ...
- 在被vue组件引用的 js 文件里获取组件实例this
思路: 通过调用函数 把 组件实例this 传递 到 被应用的 js文件里 实例: 文件结构 在SendThis.vue 文件中引用 了modalConfig.js import modalConf ...
随机推荐
- Java 面试总结(一) —— 面试常问的关键字总结
关键字: final finalize finally throws和throw static关键字的作用 abstract 和 interface super 和 this synchronize ...
- Python3中文件处理
1.txt,xls,doc等文件的使用 f=open("filename","w") 打开一个用于写入的文件,要写入内容时使用f.write("内 ...
- (转载)VB6之鼠标移出事件
转载链接:http://www.ltesting.net/ceshi/ruanjianceshikaifajishu/rjcskfyy/vb/2007/0525/3426.html Windows提供 ...
- 结巴(jieba)中文分词及其应用实践
中文文本分类不像英文文本分类一样只需要将单词一个个分开就可以了,中文文本分类需要将文字组成的词语分出来构成一个个向量.所以,需要分词. 这里使用网上流行的开源分词工具结巴分词(jieba),它可以有效 ...
- requireJS 源码(二) data-main 的加载实现
(一)requireJs 的整体结构: requireJS 源码 前192行,是一些 变量的声明,工具函数的实现 以及 对 三个全局变量(requirejs,require,define)若被占用后的 ...
- 针对双系统ubuntu16.04卡死及系统没有声音解决方法
楼主电脑系统状况:win10主系统,128固态为ubuntu系统 安装一共为两次. 第一次出现ubuntu安装成功后没有声音,主系统win10有声音,Ubuntu上检测不到声卡,说明ubu ...
- 百度云bcc建站
一.购买百度云服务 1.百度云bcc购买网页http://bce.baidu.com/product/bcc.html 2.买完后管理:http://console.bce.baidu.com/bcc ...
- 支持多个版本的ASP.NET Core Web API
基本配置及说明 版本控制有助于及时推出功能,而不会破坏现有系统. 它还可以帮助为选定的客户提供额外的功能. API版本可以通过不同的方式完成,例如在URL中添加版本或通过自定义标头和通过Accept- ...
- koa2 use里面的next到底是什么
koa2短小精悍,女人不爱男人爱. 之前一只有用koa写一点小程序,自认为还吼吼哈,知道有一天某人问我,你说一下 koa或者express中间件的实现原理.然后我就支支吾吾,好久吃饭都不香. 那么了解 ...
- (转)java for循环的执行顺序和几种常用写法
算是温习吧.问题比较基础,但是也比较重要.(虽然是C,但是很经典) for循环可以说在每个程序中都少不了的,语句头包括三个部分:初始化,判读条件,一个表达式. 但是这三个部分的执行顺序是什么,这是我们 ...