子组件改变父组件的两种方式

首先父子组件传值:很简单
子组件改变父组件的方式一:

  adddata: function () {
    this.$emit('addnum:datanum', this.datanum)
  }

通过this.$emit 的方式传递给父组件,然后在父组件中写方法,改变值。
这里有一个问题就是加上 :datanum 子组件改变不了父组件值
去掉就可以改变

<children :datanum="datan" @addnum="addnumdata"></children>
      addnumdata: function () {
        this.datan += 1
      },

方式二 使用.sync 修饰符
首先在子页面同样使用this.$emit

  adddata: function () {
    this.$emit('update:datanum',this.num)
  }

注意update 要写对 : 后面是父组件传的值(要改的属性),和值
父组件中:

<child  :datanum.sync="datan"></child>

datan 就是要改变的父组件值

我自己觉得是.sync 可以理解为 “双向绑定”,父传子是单向数据流是绑定的,但是子传父需要一定的方式 this.$emit(‘update:***’ ,***)
:datanum.sync 其实也是向子组件传值的过程
个人理解,请大家指正
prop为对象数组时不曾尝试

父组件代码:

<template>
  <div style="color: red">
    <button @click="add">点击</button>
    父页面---{{datan}}
    父页面---{{post.title}}
    父页面---{{post.num}}
    <div style="padding: 20px 0">-------------------------------------------------</div>
    <children :datanum="datan" @addnum="addnumdata"></children>
    <child  :datanum.sync="datan"></child>
  </div>
</template>

<script>
  import children from './children'
  import child from './child'

  export default {
    name: "",
    data: function () {
      return {
        msg: 'This is a Vue project',
        post: {
          num: 0,
          title: 'My Journey with Vue'
        },
        datan: 1,
      }
    },
    create: function () {
    }, /*渲染前调用*/
    mounted: function () {
    }, /*渲染后调用*/
    components: {
      child,
      children
    },// 组件注册
    methods: {
      addnumdata: function () {
        this.datan += 1
      },
      add: function () {
        this.datan += 1
      },
    }  /*方法*/
  }
</script>

<style scoped>

</style>

子组件一:

<template>
  <div>
    <div>
      <button @click="adddata()">点击</button>
      子页面--{{datanum}}
      子页面--{{datanum}}
    </div>
    <div style="padding: 20px 0">-------------------------------------------------</div>
  </div>
</template>

<script>
  export default {
    name: "",
    data: function () {
      return {
        msg: 'This is a Vue project'
      }
    },
    create: function () {
    }, /*渲染前调用*/
    props: {
      datanum: Number
    },
    mounted: function () {
    }, /*渲染后调用*/
    methods: {
      adddata: function () {
        this.$emit('addnum:datanum', this.datanum)
      }
    }  /*方法*/
  }
</script>

<style scoped>

</style>

子组件二:

<template>
  <div>
    <div>
      <button @click="adddata()">点击</button>
      子页面--{{datanum}}
      子页面--{{datanum}}
    </div>
    <div style="padding: 20px 0">-------------------------------------------------</div>
  </div>
</template>

<script>
  export default {
    name: "",
    data: function () {
      return {
        msg: 'This is a Vue project',
        num:0
      }
    },
    create: function () {
    }, /*渲染前调用*/
    props: {
      datanum: Number
    },
    mounted: function () {
    }, /*渲染后调用*/
    methods: {
      adddata: function () {
        this.$emit('update:datanum',this.num)
      }
    }  /*方法*/
  }
</script>

<style scoped>

</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 3 中,组件改变组件的值有两种常见的方式:通过事件和通过 v-model。 通过事件: 1. 在组件中,使用 `$emit` 方法触发一个自定义事件,传递需要改变的值作为参数。 2. 在组件中,通过在组件上监听该自定义事件,并在事件处理函数中更新组件的值。 示例代码: // 组件 <template> <button @click="changeValue">点击改变值</button> </template> <script> export default { methods: { changeValue() { this.$emit('update:value', newValue); } } } </script> // 组件 <template> <div> <p>{{ value }}</p> <ChildComponent @update:value="updateValue" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { value: '初始值' }; }, methods: { updateValue(newValue) { this.value = newValue; } } } </script> 通过 v-model: 1. 在组件中,使用 `v-model` 绑定一个值,并在组件内部改变这个值。 2. 在组件中,使用 `v-model` 绑定这个值,并在组件内部获取和更新这个值。 示例代码: // 组件 <template> <input v-model="internalValue" /> </template> <script> export default { model: { prop: 'value', event: 'input' }, props: { value: { type: String, required: true } }, data() { return { internalValue: this.value }; }, watch: { internalValue(newVal) { this.$emit('input', newVal); } } } </script> // 组件 <template> <div> <p>{{ value }}</p> <ChildComponent v-model="value" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { value: '初始值' }; } } </script> 这样,无论使用哪种方式组件改变组件的值都是通过事件的方式进行通信的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值