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

相关推荐
qq_5470261792 小时前
Flowable 工作流引擎
java·服务器·前端
刘逸潇20052 小时前
CSS基础语法
前端·css
Sheldon一蓑烟雨任平生3 小时前
Vue3 插件(可选独立模块复用)
vue.js·vue3·插件·vue3 插件·可选独立模块·插件使用方式·插件中的依赖注入
吃饺子不吃馅3 小时前
[开源] 从零到一打造在线 PPT 编辑器:React + Zustand + Zundo
前端·svg·图形学
小马哥编程4 小时前
【软考架构】案例分析-Web应用设计(应用服务器概念)
前端·架构
鱼与宇4 小时前
苍穹外卖-VUE
前端·javascript·vue.js
啃火龙果的兔子4 小时前
前端直接渲染Markdown
前端
z-robot4 小时前
Nginx 配置代理
前端
用户47949283569154 小时前
Safari 中文输入法的诡异 Bug:为什么输入 @ 会变成 @@? ## 开头 做 @ 提及功能的时候,测试同学用 Safari 测出了个奇怪的问题
前端·javascript·浏览器
没有故事、有酒4 小时前
Ajax介绍
前端·ajax·okhttp