($children,$refs,$parent)的使用

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

  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=0;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>

 

转载于:https://www.cnblogs.com/liubingyjui/p/10976264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue2中,使用`this.$parent.$parent.$parent.$refs`可以访问父组件的父组件的父组件的`refs`对象。引用的代码是一个示例,其中父组件中的`this.$refs["detailForm"]`指向子组件`PDetail`的实例,通过`this.$parent.$parent.$parent.$refs`可以在子组件中访问父组件的父组件的`refs`对象。具体来说,在上述代码中,父组件`Appe`通过`ref="detailForm"`将子组件`PDetail`引用为`"detailForm"`,然后在子组件中可以使用`this.$parent.$parent.$parent.$refs.detailForm`来访问父组件的`refs`对象中的`detailForm`属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue组件 $children,$refs$parent使用详解](https://download.csdn.net/download/weixin_38669091/12775877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue中this.$parent、this.$root、this.$children、this.$ref的区别和作用](https://blog.csdn.net/weixin_44684303/article/details/124748102)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值