如何使用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定义组件之------父组件调用子组件

相关推荐
子琦啊1 分钟前
华为 OD 2026年5月笔试题解析
javascript·华为
无风听海10 分钟前
Promise 与 Async Await 深度解析
前端·javascript
牛奶12 分钟前
AI 永远说好,于是我们只会说 yes
前端·aigc·ai编程
浩风祭月12 分钟前
把前端项目的 CI 构建时间从 15 分钟压到了 2 分钟
前端·ai编程
牛奶16 分钟前
黑客是怎么看到你数据的?
前端·安全·黑客
ihuyigui18 分钟前
国际企业办公短信接口
前端·后端·架构
lpd_lt31 分钟前
服务端类vue等页面AI测试方向
前端·vue.js·人工智能
AugustRed33 分钟前
A2UI 完整学习指南(含 Java 后端 + 前端实战示例)
java·开发语言·前端
王莎莎-MinerU37 分钟前
Agent 时代,科学数据 API 需要重新设计
大数据·前端·数据库·人工智能·个人开发
jingling55541 分钟前
自建技术博客实战(三):工具专栏——地图定位、声音复刻与 rembg 抠图
android·开发语言·前端·ai·nextjs