当前位置:首页 > VUE

vue props实现双向绑定

2026-02-24 05:30:38VUE

在 Vue 中实现 props 的双向绑定

Vue 的 props 默认是单向数据流,即父组件传递给子组件的数据不能直接在子组件中修改。但可以通过以下方法实现类似双向绑定的效果。

使用 v-model 语法糖

v-model 是 Vue 提供的语法糖,可以方便地实现父子组件之间的双向绑定。

父组件代码:

<template>
  <ChildComponent v-model="parentValue" />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: { ChildComponent },
  data() {
    return {
      parentValue: 'initial value',
    };
  },
};
</script>

子组件代码:

<template>
  <input :value="value" @input="$emit('input', $event.target.value)" />
</template>

<script>
export default {
  props: ['value'],
};
</script>

使用 .sync 修饰符

Vue 2.x 提供了 .sync 修饰符,可以实现 props 的双向绑定。

父组件代码:

<template>
  <ChildComponent :childProp.sync="parentValue" />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: { ChildComponent },
  data() {
    return {
      parentValue: 'initial value',
    };
  },
};
</script>

子组件代码:

<template>
  <input :value="childProp" @input="$emit('update:childProp', $event.target.value)" />
</template>

<script>
export default {
  props: ['childProp'],
};
</script>

手动实现双向绑定

如果不想使用 v-model.sync,可以手动通过事件实现双向绑定。

父组件代码:

<template>
  <ChildComponent :propValue="parentValue" @update="handleUpdate" />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: { ChildComponent },
  data() {
    return {
      parentValue: 'initial value',
    };
  },
  methods: {
    handleUpdate(newValue) {
      this.parentValue = newValue;
    },
  },
};
</script>

子组件代码:

<template>
  <input :value="propValue" @input="$emit('update', $event.target.value)" />
</template>

<script>
export default {
  props: ['propValue'],
};
</script>

使用计算属性

在子组件中,可以通过计算属性实现 props 的双向绑定效果。

子组件代码:

vue props实现双向绑定

<template>
  <input v-model="computedValue" />
</template>

<script>
export default {
  props: ['value'],
  computed: {
    computedValue: {
      get() {
        return this.value;
      },
      set(newValue) {
        this.$emit('input', newValue);
      },
    },
  },
};
</script>

注意事项

  • 直接修改 props 会导致警告,应该通过事件通知父组件更新数据。
  • v-model 默认使用 value 作为 prop 和 input 作为事件,可以通过 model 选项自定义。
  • Vue 3 中 .sync 修饰符已被移除,推荐使用 v-model 或自定义事件实现双向绑定。

标签: 绑定双向
分享给朋友:

相关文章

php实现双向队列

php实现双向队列

PHP 实现双向队列的方法 双向队列(Deque,Double-ended Queue)是一种允许在队列两端进行插入和删除操作的数据结构。PHP 中可以通过数组或 SplDoublyLinkedLis…

vue实现双向滚动

vue实现双向滚动

实现双向滚动的 Vue 方法 双向滚动通常指两个关联的滚动区域(如表格的头部和内容部分)同步滚动。以下是几种实现方式: 使用事件监听和 scroll 方法 通过监听一个滚动区域的 scrol…

vue双向实现原理

vue双向实现原理

Vue 双向数据绑定的实现原理 Vue 的双向数据绑定主要通过数据劫持结合发布-订阅模式实现,核心是 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)。…

vue怎么实现双向

vue怎么实现双向

Vue 双向绑定的实现方式 Vue 中实现双向绑定的核心机制是 v-model 指令,它结合了属性绑定和事件监听,适用于表单元素或自定义组件。 表单元素的双向绑定 在原生表单元素(如 input、s…

vue 双向绑定 实现原理

vue 双向绑定 实现原理

Vue 双向绑定的实现原理 Vue 的双向绑定是通过数据劫持结合发布者-订阅者模式实现的,核心是 Object.defineProperty(Vue 2.x)或 Proxy(Vue 3.x)。以下是具…

vue如何实现双向变电

vue如何实现双向变电

Vue 双向绑定实现方法 Vue 中实现双向绑定的核心是通过 v-model 指令,它结合了 v-bind 和 v-on 的功能,简化了表单元素与数据的同步操作。 基础用法 在表单元素上直接使用 v…