父子组件之间传值

1.父组件向子组件传值

父组件可以在引用子组件的时候,通过属性绑定(v-bind:)的形式,把需要传递给子组件的数据,通过属性绑定传递到子组件内部,供子组件使用。
当父组件通过属性绑定将值传过来时,子组件通过props:[ ‘属性值’]接受传过来的属性

父组件代码:

<template>
    <div>
       <child :parentmsg='msg'></child>//在子组件中通过自定义属性向子组件传值
    </div>
</template>
<script>
import child from './child.vue'//引入子组件
export default {
  components:{child},//注册子组件
    data() {
      return {
        msg:'我是父组件,给了子组件一个值'
      }
    },
    methods: {
    }
  }
</script>

子组件代码:

<template>
    <div>
       父组件传递过来的值:{{parentmsg}}
    </div>
</template>
<script>
export default {
    props:['parentmsg'],//通过props接收父组件传递过来的值,注意:此处的parentmsg和父组件的自定义属性parentmsg必须一致
    data() {
      return {
      }
    },
    methods: {
    }
  }
</script>

最后效果:
在这里插入图片描述

2.父组件向子组件传递方法(子组件向父组件传值)

父组件向子组件传递方法,采用的是事件绑定机制(v-on:)。当我们定义完一个事件属性之后,子组件通过this.$emit(‘方法名’)方法调用父组件传递过来的方法。

父组件代码:

<template>
    <div>
       <child @parentFunction='show'></child>//通过点击事件将父组件定义的show方法传递给子组件,parentFunction名称自定义
    </div>
</template>
<script>
import child from './child.vue'
export default {
  components:{child},
    data() {
      return {}
    },
    methods: {
      show(value1,value2){
        console.log('父组件的方法')
      }
    }
  }
</script>

子组件代码:

<template>
    <div>
       <button @click="childClick()">子组件按钮</button>
    </div>
</template>
<script>
export default {
    props:['parentmsg'],
    data() {
      return {
      }
    },
    methods: {
        childClick(){
            this.$emit('parentFunction',value1,value2) //this.$emit的第一个参数是父组件传递过来的方法,后面的所有参数都是子组件向父组件传递的值
        }
    }
  }
</script>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,父组件与子组件之间传值有多种方式: 1. Props: 父组件通过props向子组件传递数据。在父组件中使用子组件时,可以通过属性的形式将数据传递给子组件。子组件通过props选项来接收父组件传递的数据。例如: ```vue // 父组件 <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' }; } }; </script> // 子组件 <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: ['message'] }; </script> ``` 2. $emit: 子组件通过$emit方法向父组件发送事件,并携带需要传递的数据。父组件监听子组件触发的事件,并在事件处理函数中获取子组件传递的数据。例如: ```vue // 父组件 <template> <div> <child-component @custom-event="handleCustomEvent"></child-component> <p>Message from child: {{ childMessage }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { childMessage: '' }; }, methods: { handleCustomEvent(message) { this.childMessage = message; } } }; </script> // 子组件 <template> <div> <button @click="sendMessage">Send Message to Parent</button> </div> </template> <script> export default { methods: { sendMessage() { this.$emit('custom-event', 'Hello from child'); } } }; </script> ``` 这些是Vue中父子组件之间传值的两种常见方式,根据具体场景选择合适的方式进行数据传递。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值