vue零散记录之父子组件传值

本文介绍Vue.js中父组件与子组件间的通信方式,包括如何通过$emit实现子组件向父组件传递值,以及父组件如何接收这些值。同时,还探讨了输入框使用v-model时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.input 使用了v-mode绑定值,不需要在写value;

2.子组件传值给父组件:$emit

父组件可获取子组件值:

举例:

父组件:parentServe.vue

<template>
  <div>
    <child-serve   @colorShow="ifshowColor"><!-- 这里是子组件传来的事件colorShow-->
    </child-serve>
  </div>
</template>

<script>
import childServev from "../components/childServe"

export default {
  methods:{
    showAdvice(advice){
      alert(advice)
    },
  },
  components:{
    child-serve:"childServe"
  },}
</script>

子组件:childServe

<template>
  <button @click="handleShowColor">
    我想弹窗传值给父组件呢
  </button>
</template>

<script>
  export default {
    data () {
      return {
        colorShow:false
    },
    methods:{

     handleShowColor (event) {

      event.stopPropagation();

      if (this.colorShow) {

        this.colorShow = false

      } else {

        this.colorShow = true

      }

       this.$emit("colorShow",this.colorShow);//colorShow是事件,this.colorShow是值

    },


  }
</script>

3.父组件传值给子组件的时候,是静态传值,如果子组件做操作,需要发起事件执行。

举例:-待补充

4.扩展知识:父子组件的加载顺序 - 按顺序执行,

如:先加载父组件,如过程发现子组件则执行子组件。最后输出结果。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值