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

随机推荐
- [ZJOI2011] 最小割 - 最小割树
最小割树裸题 建树后,以每个点为根跑DFS求出距离矩阵,然后暴力回答询问即可 #include <bits/stdc++.h> using namespace std; #define i ...
- Xlrd模块读取Excel文件数据
Xlrd模块使用 excel文件样例:
- 【C/C++】最短路径
BFS求无权图的最短路径 用book数组的值表示路径长度即可,省略 Floyd算法(允许负边) Floyd算法可以一次性求出所有节点之间的最短距离,且代码简单,但是时间复杂度达到了n^3,因此只适用于 ...
- JDBC——ResultSet结果集对象
ResultSet结果集对象,封装结果.它是怎么做到封装结果的呢? 游标,类似指针索引最初指在“列名”上,要取到数据就需要让游标向下移动移动后就指向了第一行数据,然后通过一些方法把第一行的每一列都取出 ...
- apache配置跨域请求代理
1.配置允许跨域请求 Header always set Access-Control-Allow-Origin "*"Header always set Access-Contr ...
- 文艺平衡树 lg3391(splay维护区间入门)
splay是支持区间操作的,先做这道题入个门 大多数操作都和普通splay一样,就不多解释了,只解释一下不大一样的操作 #include<bits/stdc++.h> using name ...
- 基于Java在线学习系统设计与实现
Spring+SpringMVC+MyBatis+Bootstrap+Vue开发在线学习系统 本课题的主要内容是开发基于Java EE的在线学习平台,使用MVC经典开发模式. ...
- Wooden Sticks(贪心)
Wooden Sticks. win the wooden spoon:成为末名. 题目地址:http://poj.org/problem?id=1065 There is a pile of n w ...
- ubuntu安装搜狗输入
百度搜索搜狗输入ubuntu找到官网地址 下载deb包 sogoupinyin_2.3.1.0112_amd64.deb 上传 dkpkg -i sogoupinyin_2.3.1.0112_a ...
- 空指针和NULL
#include <stdio.h> int add(int a, int b ){ //函数的返回值和参数意一样有副本机制,存储在寄存器中,而不在内存中,函数的返回值不能取地址 & ...