二十一、父子组件的通信(父子组件的访问方式)
有时候需要父组件直接访问子组件,子组件直接访问父组件,或者是子组件访问根组件
- 父组件访问子组件:使用 $children 或 $refs (renference--引用)
- 子组件访问父组件:使用 $parent
一、父访问子
1. this.$children 是一个数组类型,它包含所有子组件对象
我们这里通过一个遍历,取出所有子组件的message状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <div id= "app" > <my_cpn></my_cpn> <my_cpn></my_cpn> <my_cpn></my_cpn> <button @click= "btnClick" >按钮</button> </div> <template id= "cpnC" > <div>我是子组件</div> </template> <script> const app = new Vue({ el: "#app" , data: { message: "Hellow World" }, methods: { btnClick(){ // 1. $children => 数组类型 console.log( this .$children); for ( let c of this .$children){ console.log(c.name); } // 一般不适用下标值拿数据 this .$children[0].showMessage(); } }, components: { my_cpn: { template: "#cpnC" , data(){ return { name: "我是子组件的name" } }, methods: { showMessage(){ console.log( "showMessage" ) } } } } }); </script> |
2. this.$refs 是一个对象类型,它默认是一个空对象 包含所有子组件对象,需要在模板引用标签上加上 ref="key" 使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <div id= "app" > <my_cpn></my_cpn> <my_cpn></my_cpn> <my_cpn ref = "aaa" ></my_cpn> <button @click= "btnClick" >按钮</button> </div> <template id= "cpnC" > <div>我是子组件</div> </template> <script> const app = new Vue({ el: "#app" , data: { message: "Hellow World" }, methods: { btnClick(){ // 1. $children => 数组类型 // console.log(this.$children); // for(let c of this.$children){ // console.log(c.name); // } // 一般不适用下标值拿数据 //this.$children[0].showMessage(); //2. $refs =>对象类型,默认是一个空对象 // 需要在模板引用标签上加上 ref="key" 使用 console.log( this .$refs.aaa.name) } }, components: { my_cpn: { template: "#cpnC" , data(){ return { name: "我是子组件的name" } }, methods: { showMessage(){ console.log( "showMessage" ) } } } } }); </script> |
二、子访问父
1. this.$parent 访问父类组件
2. this.$root 访问根组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <div id= "app" > <my_cpn></my_cpn> </div> <template id= "cpnC" > <div> <p>我是子组件</p> <button @click= "btnClick" >按钮</button> <d_cpn></d_cpn> </div> </template> <template id= "cpnD" > <div> <p>我是子组件的子组件</p> <button @click= "btnClick" >按钮</button> </div> </template> <script> const app = new Vue({ el: "#app" , data: { message: "Hellow World" }, components: { my_cpn: { template: "#cpnC" , data(){ return { name: "我是二级的组件" } }, methods: { btnClick(){ // 1.访问父组件$parent console.log( this .$parent) } }, components: { d_cpn: { template: "#cpnD" , methods: { btnClick(){ //访问父组件 console.log( this .$parent); console.log( this .$parent.name); //访问根组件 console.log( this .$root); console.log( this .$root.message); } } } } } } }); </script> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现