【vue父子组件传值】

父组件传子组件

<div id='app'>
        <!-- 父传子需要在父组件中的子组件中定义一个变量 -->
        <son :type="father"></son>
    </div>

    <template id="son">
        <div>
            {{type}}
        </div>
    </template>

    <script>
        // 子组件
        Vue.component('son',{
        template:'#son',
        // 在子组件中需要用props接收父组件的值
        props:{
            type:[String,Number]
        },
    })
    // 父组件
    const vm = new Vue({
        el: '#app',
        data: {
            father:'父组件传给子组件的值'
        },
        methods: {           
        }
    })
    </script>

子组件传父组件

<div id='app'>
        <!-- 子传父需要在父组件中的子组件中定义一个自定义事件 -->
        <son @getson="getson"></son>
        <h2>{{msg}}</h2>
    </div>
    <template id="son">
    </template>
    <script>
        // 子组件
        Vue.component('son',{
            template:'#son',
            data(){
                return {
                    msg:'子传过来的值'
                }
            },
            created(){
                // 在子组件中用this.$emit传递参数   this.$emit(第一个参数 自定义事件的名字,第二个参数  传递的值) 
                this.$emit('getson',this.msg)
            }
        })

        // 父组件
    const vm = new Vue({
        el: '#app',
        data: {
            msg:''
        },
        methods: {
            // 自定义的事件名函数接收子传过来的值
            getson(data){
                this.msg = data
            }
        }
    })
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建用户界面。在Vue中,父子组件之间传递数据是一种常见的需求。以下是一种常用的方法来实现父子组件之间的数据传递: 1. Props(属性):父组件可以通过props属性向子组件传递数据。在父组件中,通过在子组件标签上绑定属性的方式传递数据。在子组件中,可以通过props选项接收并使用这些数据。 父组件: ```html <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent component' }; } } </script> ``` 子组件: ```html <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: ['message'] } </script> ``` 2. $emit(自定义事件):子组件可以通过$emit方法触发自定义事件,并将需要传递的数据作为参数传递给父组件。在父组件中,通过在子组件标签上监听自定义事件的方式接收数据。 父组件: ```html <template> <div> <child-component @child-event="handleChildEvent"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleChildEvent(data) { console.log(data); // 在这里处理子组件传递的数据 } } } </script> ``` 子组件: ```html <template> <div> <button @click="emitEvent">触发事件</button> </div> </template> <script> export default { methods: { emitEvent() { this.$emit('child-event', 'Hello from child component'); // 触发自定义事件,并传递数据给父组件 } } } </script> ``` 以上是Vue中实现父子组件之间传递数据的两种常用方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值