子组件访问父组件的父组件的ref

框架:vue
需求:在子组件中需要调用祖父组件的ref下的封装的方法。
解决办法:使用$root调用根组件,然后层层往下剥。
代码:
1.首先看解决方法。该方法是在子组件的method中调用。

checkAboutMe() {
    
    
      //用了$root去访问根组件,然后通过根组件往下寻找方法
      this.$root.$children[0].$children[0].$refs.carouselFull.setActiveItem(1);
    }

2.具体分层
第一层:this.$root指代的是根组件
第二层:this.$root.$children就是指代的是app.vue组件
第三层:this.$root.$children[0].$children就是指代的是祖父组件。
第四层:this.$root.$children[0].$children[0].$refs就是祖父组件的引用。
第五层:this.$root.$children[0].$children[0].$refs.carouselFull祖父组件的具体引用。
第六层:this.$root.$children[0].$children[0].$refs.carouselFull.setActiveItem(1)祖父组件的具体引用下的方法。

总结:this.$root是比app.vue更高一级的组件的存在。

猜你喜欢

转载自blog.csdn.net/qq_43234632/article/details/106807328