Vue父子组件之间属性访问

本文介绍了Vue中父子组件之间的属性访问方法,包括父组件如何通过this.$children和this.$refs访问子组件属性,以及子组件如何利用this.$parent和this.$root访问父组件和根组件的属性。详细阐述了$children和$refs的区别,以及使用注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.父子组件之间属性访问

通常父子组件是可以进行属性访问的,例如父组件调用子组件中的方法,变量等,这些都是可以通过拿到各自对应的父子组件对象,然后调用其属性,例如:
父组件访问子组件中的属性有两种方式

  1. 在父组件中使用this.$children拿到所有的子组件数组对象,然后索引访问对应子组件中的方法,变量等属性
  2. 通过this.$refs(子组件名称来访问),这种相比于第一种就是可以通过子组件的具体名称来访问了,避免索引顺序改变问题
1.1 父组件访问子组件(this.$children,this.$refs)

1.$children获取子组件对象列表

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>父子组件访问</title>
        <script src="./vue.js"></script>
        <style>
            [v-cloak] {
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
            <component1></component1>
            <button @click="btnclick">按钮</button>
        </div>

        <template id="content">
            <div>
                <span>子组件中的内容</span>
            </div>
        </template>
    
        <script>
            const vm = new Vue({
                el:"#app",
                data:{

                },
                methods:{
                    btnclick(){
                        console.log(this.$children); // this.$children返回的数所有子组件对象的数组
                        this.$children[0].showMessage()
                    }
                },
                components:{
                    component1:{
                        template: '#content',
                        methods:{
                            showMessage(){
                                console.log("打印子组件中的showMessage函数");
                            }
                        }
                    }
                }
            })
        </script>
    </body>
</html>

在浏览器控制台我们可以看到打印结果如下:
在这里插入图片描述
注意点:父组件中调用btnclick()方法中通过this.$children可以获取所有的子组件对象列表,然后通过索引获取第一个子组件对象调用子组件中的showMessage()方法

2.this.$refs
使用$refs时,首先我们需要给组件添加一个ref属性,然后this.$refs返回一个包含组件名称的对象,然后通过属性名称拿到对应子组件

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>父子组件访问</title>
        <script src="./vue.js"></script>
        <style>
            [v-cloak] {
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
            <component1 ref="cmp1"></component1>
            <component1 ref="cm2"></component1>
            <component1 ref="cmp3"></component1>
            <button @click="btnclick">按钮</button>
        </div>

        <template id="content">
            <div>
                <span>子组件中的内容</span>
            </div>
        </template>
    
        <script>
            const vm = new Vue({
                el:"#app",
                data:{

                },
                methods:{
                    btnclick(){
                        console.log(this.$refs.cmp1); // this.$refs返回的所有子组件名称对象的对象
                        console.log(this.$refs.cmp1); // 
                        this.$refs.cmp1.showMessage()
                    }
                },
                components:{
                    component1:{
                        template: '#content',
                        methods:{
                            showMessage(){
                                console.log("打印子组件中的showMessage函数");
                            }
                        }
                    }
                }
            })
        </script>
    </body>
</html>

在这里插入图片描述

注意点:要使用this. r e f s 方 法 时 首 先 要 在 组 件 红 给 子 组 件 添 加 一 个 r e f 属 性 来 区 分 不 同 子 组 件 , 然 后 t h i s . refs方法时首先要在组件红给子组件添加一个ref属性来区分不同子组件,然后this. refsrefthis.refs可以获取到名称为ref的,值为对应组件对象的对象,最后可以通过this.$refs.子组件属性名称.方法来调用子组件中对应属性

1.2 子组件访问父组件属性(this.$parent, this.$root)

上面时父组件获取子组件中的属性,那么子组件也是可以获取父组件对象或跟组件对象的
1.this. p a r e n t 获 取 父 组 件 对 象 并 访 问 属 性 2. t h i s . parent 获取父组件对象并访问属性 2.this. parent访2.this.root 获取跟组件对象

this.$parent

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>父子组件访问</title>
        <script src="./vue.js"></script>
        <style>
            [v-cloak] {
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
            <span>root组件中的内容</span>
            <component1 ref="cmp1"></component1>
        </div>

        <template id="content">
            <div>
                <span>父组件中component1的内容</span>
                <component2></component2>
            </div>
        </template>

        <template id="content2">
            <div>
                <span>子组件component2中的内容</span>
                <button @click="cmpclick">按钮</button>
            </div>
        </template>
    
        <script>
            const vm = new Vue({
                el:"#app",
                data:{

                },
                methods:{
                },
                components:{
                    component1:{
                        template: '#content',
                        data(){
                            return {name: 'Jack'}
                        },
                        methods:{
                            btnclick(){
                                console.log("打印component1组件中的showMessage函数");
                            }
                        },
                        components:{
                            component2:{
                                template: '#content2',
                                methods:{
                                    cmpclick(){
                                        console.log(this.$parent, this.$parent.name);
                                        this.$parent.btnclick();
                                    }
                                }
                            }
                        }
                    }
                }
            })
        </script>
    </body>
</html>

通过子组件中在定义子组件,然后通过this.$parent拿到父组件对象,但是这种方式耦合度很高,一般不常用

this.$root获取root根组件

components:{
                    component1:{
                        template: '#content',
                        data(){
                            return {name: 'Jack'}
                        },
                        methods:{
                            btnclick(){
                                console.log("打印component1组件中的showMessage函数");
                            }
                        },
                        components:{
                            component2:{
                                template: '#content2',
                                methods:{
                                    cmpclick(){
                                        // console.log(this.$parent, this.$parent.name);
                                        // this.$parent.btnclick();
                                        console.log(this.$root, this.$root.message);
                                        
                                    }
                                }
                            }
                        }
                    }
                }
Vue项目中,父子组件之间传值有多种方法可以实现。其中一种常用的方法是使用props和$emit。 首先,在父组件中,可以通过props属性将数据传递给子组件。在子组件中,通过props选项接收父组件传递的数据。例如,在父组件中定义一个子组件,并通过props属性传递一个名为"name"的数据给子组件: ```html <Child :name="小张"></Child> ``` 在子组件中,通过props选项接收父组件传递的数据: ```javascript props: \["name"\] ``` 另一种方法是使用$emit来自定义事件,在子组件中触发该事件并传递数据给父组件。在父组件中,通过监听子组件触发的事件来获取子组件传递的数据。例如,在子组件中触发一个名为"increment"的自定义事件,并传递数据"我是子组件"给父组件: ```javascript this.$emit("increment", "我是子组件") ``` 在父组件中,通过监听子组件触发的事件来获取子组件传递的数据: ```html <Child @increment="f1"></Child> ``` ```javascript methods: { f1(data) { console.log(data) // 打印"我是子组件" } } ``` 这样,父组件就可以接收到子组件传递的数据了。 除了props和$emit,还有其他方法可以实现父子组件之间的传值,如使用$parent和$children来访问父组件子组件的实例,或者使用$ref来引用子组件。但是在实际开发中,props和$emit是最常用的方法。 #### 引用[.reference_title] - *1* *3* [vue父子组件传值的方法](https://blog.youkuaiyun.com/YoungMan_09/article/details/123451827)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Vue父子组件传值](https://blog.youkuaiyun.com/qq_49867247/article/details/123480614)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值