如果项目很大,组件很多,怎么样才能准确的、快速的寻找到我们想要的组件了??

  1. $refs
    首先你的给子组件做标记。demo :<firstchild ref="one"></firstchild>
    然后在父组件中,通过this.$refs.one就可以访问了这个自组件了,包括访问自组件的data里面的数据,调用它的函数
    注意
    ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素; 如果用在子组件上,引用就指向组件实例
    当 v-for 用于元素或组件的时候,引用信息将是包含 DOM 节点或组件实例的数组

  2. $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>

随机推荐

  1. spring框架中用到了哪些设计模式

    1.代理模式:在AOP和remoting中被用的比较多 2.单例模式:在spring配置文件中定义的bean默认为单例模式 3.模板方法模式:解决代码重复问题 4.前端控制器模式:spring提供了D ...

  2. vue中jquery详情

    jQuery基本语法 $(selector).action() 基本选择器:$("#id") 标签选择器:$("tagName") class选择器:$(&qu ...

  3. 【已解决】使用 yarn 安装时,报错node_modules\node sass:Command failed.

    npm install -g mirror-config-china --registry=http://registry.npm.taobao.org npm install node-sass y ...

  4. Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1 SpringBoot发送邮件

    解决方案 换端口 QQ邮箱可以把端口换成587 设置属性 spring.mail.properties.mail.smtp.ssl.enable=true 原因 465端口是为SMTPS(SMTP-o ...

  5. npm 升级到最新版本

    先npm -v查看自己的npm 是否是最新版本,如果不是则进入安装node的文件夹,可通过 where node 查找该文件夹. 进入之后使用: npm i npm -g 之后使用: npm -v 查 ...

  6. Winform form窗体已弹出框的形式出现并回传值

    From2(弹出框)回传数据到From1 Form1(数据接收form): public string Sstr; private void button1_Click(object sender, ...

  7. 自定义Nginx返回页面

    1.403返回页面 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log noti ...

  8. MFC线程间消息传递(转)

    原文地址:https://blog.csdn.net/qq_37059136/article/details/84972192

  9. 164.扩展User模型-继承AbstractUser

    继承自AbstractUser: 如果Abstractuser中定义的字段不能够满足你的项目的要求,并且不想要修改原来User对象上的一些字段,只是想要增加一些字段,那么这时候可以直接继承自djang ...

  10. 0120 springboot集成Mybatis和代码生成器

    在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...