vue的父与子组件的访问
父访问子 (需要掌握)
this.$children 和 this.$refs
this.$children 返回的是一个数组,包含父组件的所有子组件 this,$refs 返回的是一个对象,默认为空,返回父组件中所有标签带有ref属性的子组件
this.$children 是获取父组件的所有子组件,然后返回一个数组 (不常用)
this.$refs 则是通过给 组件标签上 加上一个 ref属性,然后在父组件中调用 this.$refs时才会返回 一个对象 (常用,用的非常多 基本都是用这个)
例如 子组件标签 <cpn ref="mm"></cpn> 通过this.$refs 他就会返回一个对象 {mm:Vuecomponent} 而mm就作为一个key,我们通过this.$refs.mm 就能拿到这个组件,之后就可以拿到具体的值了
子访问父 (不常用)
this.parent 访问父组件 this.root 访问根组件 (都用得少)
本文来自博客园,作者:没有烦恼的猫猫,转载请注明原文链接:https://www.cnblogs.com/maomao777/p/15820546.html

浙公网安备 33010602011771号