($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>

随机推荐
- java序列化与反序列化的使用
个人博客 地址:http://www.wenhaofan.com/article/20180925214701 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字 ...
- 3ds Max File Format (Part 1: The outer file format; OLE2)
The 3ds Max file format, not too much documentation to be found about it. There are some hints here ...
- url跳转问题
window.history.replaceState(null, null, "/callPlanning/1") //替换路径不刷新页面 window.location.rep ...
- 巨杉Tech | 使用 SequoiaDB 分布式数据库搭建JIRA流程管理系统
介绍 JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域.很多企业与互联网公司都在使用Jira作为内部 ...
- Python中super的用法【转载】
Python中super的用法[转载] 转载dxk_093812 最后发布于2019-02-17 20:12:18 阅读数 1143 收藏 展开 转载自 Python面向对象中super用法与MRO ...
- itext操作表单域导出PDF,俗称扣模板
/** * templateUrl 模板文件路径,包含文件名 * targetUrl 目标路径 * dateMap 填充数据 */public class CreatePdfUtil { public ...
- 题解【洛谷P2003】平板
题面 由于本题中\(n\)很小,\(\Theta(n^2)\)的暴力也可以通过. 具体可参照洛谷题解区 #include <bits/stdc++.h> #define itn int # ...
- 3 种比较 cmp
结构体中的比较 struct dian{ int l,r; bool operator <(const dian &t)const { if(r==t.r) return l>t. ...
- Linux system basic 2 + add kernel for Jupyter
Linux install LibreOffice: install LibreOffice: install command: sudo apt install ./XXX.deb PS: don' ...
- WPF学习笔记二之依赖属性
1.快捷生成依赖属性:propdp然后按两次tab键 2.应用场景:自定义控件 什么是依赖属性:依赖属性自己没有值,通过依赖别人(如Binding)来获得值. 依赖属性为什么会出现:控件常用字段有限, ...