如何使用vue定义组件之——子组件调用父组件数据

1.定义父子模板template

javascript 复制代码
 <div class="container">
        <my-father></my-father>
        <my-father></my-father>
        <my-father>

        </my-father>

        <!-- 此处无法调用子组件,子组件必须依赖于父组件进行展示 -->
        <!-- <my-children></my-children> -->
    </div>


    <template id="father">
        <div>
            <h3>我是父组件</h3>
            <h3>访问子组件的数据:</h3>
            <h3>{{ msg }}</h3>
            <h3>{{ name }}</h3>
            <h3>{{ age }}</h3>
            <h3>{{ user }}</h3>

            <my-children></my-children>
            <hr>

        </div>
    </template>

    <template id="children">
        <div>
            <h6>我是子组件</h6>
            <h6>访问子组件的数据:</h6>
        </div>
    </template>

2.创建Vue实例,如何建立父子关系

javascript 复制代码
<script>
    new Vue({
        el: '.container',
        components: {
            'my-father': {//父组件
                template: '#father',
                data() {
                    return {
                        msg: "",
                        name: "",
                        age: null,
                        user: {
                            id: null,
                            username: ""
                        }
                    }
                },
                components: {
                    'my-children': { //子组件,只能在 my-father中调该组件
                        template: '#children',
                        data() {
                            return {
                                msg: "welcome children!",
                                name: "I'm a child!",
                                age: 6,
                                user: {
                                    id: 1001,
                                    username: 'admin'
                                }
                            }
                        }
                    }
                }
            }
        }
    })
</script>

技术:事件监听+事件触发

父组件在调用子组件时,监听子组件触发的自定义事件,并在父组件中定义回调方法,用来接收数据
javascript 复制代码
    <template id="father">
        <div>
            <h3>我是父组件</h3>
            <h3>访问子组件的数据:</h3>
            <h3>{{ msg }}</h3>
            <h3>{{ name }}</h3>
            <h3>{{ age }}</h3>
            <h3>{{ user }}</h3>

            <!-- 监听子组件触发的数据 -->
            <my-children @e-child="getMsg"></my-children>
            <hr>
        </div>
    </template>
在子组件中使用vm.$emit(事件名,数据)触发自定义事件,将当前获取的数据,传给父类
javascript 复制代码
                components: {
                    'my-children': { //子组件,只能在 my-father中调该组件
                        template: '#children',
                        data() {
                            return {
                                msg: "welcome children!",
                                name: "I'm a child!",
                                age: 6,
                                user: {
                                    id: 1001,
                                    username: 'admin'
                                }
                            }
                        },
                        created() {
                            //触发事件,向父组件传递数据
                            this.$emit('e-child', this.msg,this.name,this.age,this.user)
                        }
                    }
                }

父类定义一个方法,获取数据:

javascript 复制代码
new Vue({
        el: '.container',
        components: {
            'my-father': {//父组件
                template: '#father',
                data() {
                    return {
                        msg: "",
                        name: "",
                        age: null,
                        user: {
                            id: null,
                            username: ""
                        }
                    }
                },
                methods: {
                    getMsg(msg,name,age,user) {
                        this.msg = msg;
                        this.name = name;
                        this.age = age;
                        this.user = user;
                    }
                }
            }
        }
    })

打印结果:

相关文章:

如何使用vue定义组件之------全局or局部

如何使用vue定义组件之------父组件调用子组件数据

如何使用vue定义组件之------父组件调用子组件

相关推荐
wuhen_n2 分钟前
JavaScript数据结构深度解析:栈、队列与树的实现与应用
前端·javascript
我是一只puppy8 分钟前
使用AI进行代码审查
javascript·人工智能·git·安全·源代码管理
颜酱8 分钟前
从二叉树到衍生结构:5种高频树结构原理+解析
javascript·后端·算法
狗哥哥23 分钟前
微前端路由设计方案 & 子应用管理保活
前端·架构
TT哇1 小时前
【实习 】银行经理端两个核心功能的开发与修复(银行经理绑定逻辑修复和线下领取扫码功能开发)
java·vue.js
前端大卫1 小时前
Vue3 + Element-Plus 自定义虚拟表格滚动实现方案【附源码】
前端
却尘1 小时前
Next.js 请求最佳实践 - vercel 2026一月发布指南
前端·react.js·next.js
ccnocare1 小时前
浅浅看一下设计模式
前端
Lee川1 小时前
🎬 从标签到屏幕:揭秘现代网页构建与适配之道
前端·面试
Ticnix2 小时前
ECharts初始化、销毁、resize 适配组件封装(含完整封装代码)
前端·echarts