如何使用vue定义组件之——父组件向子组件传递数据

首先,准备父子容器:

 <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>
            <my-children></my-children>
            <hr>
        </div>
    </template>

    <template id="children">
        <div>
            <h5>我是子组件</h5>
        </div>
    </template>

准备Vue实例:

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

现在,我们需要向子组件索取数据:

技术:属性绑定+数据拦截

父组件在调用子组件时,以属性绑定的方式将要传递的数据绑定在子组件标签上
    <template id="father">
        <div>
            <h3>我是父组件</h3>
            <h3>访问自己的数据:</h3>
            <h3>{{ msg }}</h3>
            <!-- 1.向子组件绑定数据 -->
            <my-children v-bind:father_name="name" :age="age" :user="user"></my-children>
            <hr>
        </div>
    </template>
在子组件对象中,使用props选项声明获取的数据,进行绑定属性的拦截,即接收来自父组件的数据
components: {
                    'my-children': { //子组件,只能在 my-father中调该组件
                        template: '#children',
                        props: {
                            //接受父组件传递过来的数据
                            //不能使用fatherName接受数据!!!!
                            // fatherName: String
                            father_name: {
                                type: String,
                                default: "父类的元素"
                            },
                            age: {
                                type: Number,
                                default: 100
                            },
                            user: {
                                type: Object,
                                default :function() {
                                    return {
                                        id: 1000,
                                        username: "默认名"
                                    }
                                }
                            }
                        }
                    }
                }

现在,可以在界面调用父组件的数据:

    <template id="children">
        <div>
            <h5>我是子组件</h5>
            <h4>访问父组件的数据:{{ father_name }}</h4>
            <h4>访问父组件的数据:{{ age }}</h4>
            <h4>访问父组件的数据:{{ user }}</h4>
        </div>
    </template>

打印结果:

相关文章:

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

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

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,子组件可以通过使用`$emit`方法来向组件发送消息并控制组件的变量。首先,在子组件定义一个自定义事件,可以使用`$emit`方法将需要传递的参数发送给组件。例如,在子组件中可以使用以下方式触发事件: ```javascript this.$emit('customEvent', data); ``` 其中,`customEvent`是自定义事件名称,`data`是需要传递给组件的参数。 然后,在组件中,可以通过使用`v-on`指令来监听子组件触发的事件,然后调用相应的方法来处理数据或更新组件的变量。例如: ```html <child-component v-on:customEvent="handleEvent"></child-component> ``` 其中,`handleEvent`是组件中处理事件的方法名称。 在`handleEvent`方法中,可以接收到子组件传递过来的参数,并进行相应的操作。例如: ```javascript methods: { handleEvent(data) { // 在这里可以对组件的变量进行操作 } } ``` 通过以上步骤,子组件就可以通过触发自定义事件,将参数传递给组件并控制组件的变量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue组件修改组件中变量的值——实现子组件控制组件弹出对话框](https://blog.csdn.net/baidu_35800355/article/details/103997973)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值