父组件使用子组件
一、父组件中访问子组件的对象去访问子组件中的方法和属性:
1.this.$children 返回数组类型
2.this.$children[0].sname 通过索引去访问子组件的对象并使用属性
3.this.$children[0].SayHellow() 调用子组件的方法
缺陷:访问局限性,通过索引,不容易确切指向某一个子组件的对象
二、使用this.$refs
1.this.$refs 返回是一个对象 需要在子组件标签中声明 ref=“name” 才能被访问到
2.this.$refs.num2Cmp.SayHellow() 所以 refs的使用是通过Key值 也就是子组件标签中 ref=name的值进行访问到具体的子组件对象的
3.访问属性console.log(this.$refs.num2Cmp.sname);
三、子组件中访问父组件的属性和方法:
1.this.$parent.name
2.this.$parent.SayHi()
访问根(父)组件
’