($children,$refs,$parent)的使用
如果项目很大,组件很多,怎么样才能准确的、快速的寻找到我们想要的组件了??
$refs
首先你的给子组件做标记。demo :<firstchild ref="one"></firstchild>
然后在父组件中,通过this.$refs.one就可以访问了这个自组件了,包括访问自组件的data里面的数据,调用它的函数
注意:
ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素; 如果用在子组件上,引用就指向组件实例
当 v-for 用于元素或组件的时候,引用信息将是包含 DOM 节点或组件实例的数组$children
他返回的是一个组件集合,如果你能清楚的知道子组件的顺序,你也可以使用下标来操作;
3.$parent在子组件中调用父组件的方法或获得其数据
parents.vue
<template>
<div id='parents'>
<p>我是父组件
<button @click="click1hanlde">获取子组件1的数据与方法</button>
<button @click="click2hanlde">获取所有子组件的数据和方法</button></p>
<childCom1 ref="childCom1"></childCom1>
<childCom2 ref="childCom2"></childCom2>
</div>
</template>
<script>
import childCom1 from './childCom1.vue'
import childCom2 from './childCom2.vue'
export default {
components:{
childCom1, childCom2
},
data() {
return {
ParentData:'AAA'
};
},
methods:{
click1hanlde(){
console.log(this.$refs.childCom1.children_data);//children_data1
this.$refs.childCom1.children_fun();
},
click2hanlde(){
for(let i=;i<this.$children.length;i++){
// console.log(this.$children.length);//2个组件 childCom1 childCom2
console.log(this.$children[i].children_data); //children_data2
this.$children[i].children_fun();
}
},
showParentData(){
console.log(this.ParentData)
}
}
};
</script>
childCom1.vue
<template>
<div id='children1'>
<p>我是子组件1: <button @click="getParent_fun">获取父组件的数据和方法 </button></p>
</div>
</template>
<script>
export default {
data() {
return {
children_data:'children_data1',
};
},
methods:{
children_fun(){
console.log('我是子组件1的方法')
},
getParent_fun(){
this.$parent.showParentData();
}
}
};
</script>
childCom2.vue
<template>
<div id='children2'>
<p>我是子组件2</p>
</div>
</template>
<script>
export default {
data() {
return {
children_data:'children_data2',
};
},
methods:{
children_fun(){
console.log('我是子组件2的方法')
}
}
};
</script>

随机推荐
- CTF之图片隐写术解题思路
参考大佬的博客:https://blog.csdn.net/a_small_rabbit/article/details/79644078 深有感触,觉得写得比较全,因此将此篇文章记录在此方便以后用得 ...
- 在Unity5中使用C#脚本实现UI的下滑、变色、渐隐渐现效果
一.首先,我们先创建一个Text 依次选择Component→UI→Text创建一个Text,创建完成后如下: 二.创建完成后,在Project面板点击Create→C# Script,本例命名 ...
- FormData控制台打印为空及使用方法
之前使用formData都是在network中查看参数,最近在做一个项目,接口还没有,用的假数据做的交互,突发奇想的console.log了 一下,结果是空的. 一开始以为append失效了,经过查证 ...
- jQuery-File-Upload 使用,jQuery-File-Upload上传插件
================================ ©Copyright 蕃薯耀 2020-01-10 https://www.cnblogs.com/fanshuyao/ 一.官网地址 ...
- 使用TensorFlow训练模型的基本流程
本文已在公众号机器视觉与算法建模发布,转载请联系我. 使用TensorFlow的基本流程 本篇文章将介绍使用tensorflow的训练模型的基本流程,包括制作读取TFRecord,训练和保存模型,读取 ...
- centos7中 yum的安装
自己误将yum卸载, 在重装时由于依赖问题一直报错: error: Failed dependencies: /usr/bin/python is needed by yum-3.4.3-16 ...
- PHP程序员应该如何提升
PHP程序员应该如何提升 尤其不认可W3school之类的东西,不够深度,理解不深,比起这个更建议看官方文档,中文不清楚,看英文的. 入门视频:入门视频推荐:哈佛大学公开课:构建动态网站Beginne ...
- gets(), getline(), cin.getline()
gets(str), getline(cin, s), cin.getline(str, len),这三个函数都是读入一行字符串的函数,下面是这三个函数的区别 1. gets() 函数是 C 语言的函 ...
- Application Comparison Of LED Holiday Light And Traditional Incandescent Lights
Obviously, LED holiday lights are leading the competition in economical Christmas decorations, but t ...
- soundtouch change pitch matlab implementation
function output = changePitch(input, pitchInSemitones) % one octave is 12 semitones octave = pitchIn ...