当前位置:首页 > 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 的双向绑定。

vue 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,可以手动通过事件实现双向绑定。

vue props实现双向绑定

父组件代码:

<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 的双向绑定效果。

子组件代码:

<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 或自定义事件实现双向绑定。

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

相关文章

vue实现数组绑定

vue实现数组绑定

Vue 数组绑定的实现方法 Vue 提供了多种方式实现数组的响应式绑定,确保数据变化时视图自动更新。以下是常见的实现方法: 使用 v-for 指令渲染数组 通过 v-for 指令可以遍历数组并渲染…

vue实现绑定页面

vue实现绑定页面

Vue 实现数据绑定到页面的方法 Vue.js 通过数据绑定机制实现数据和页面的同步更新,主要依赖响应式系统和模板语法。以下是几种常见的绑定方式: 双向绑定 (v-model) 使用 v-model…

vue双向绑定如何实现

vue双向绑定如何实现

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

vue怎样实现数组绑定

vue怎样实现数组绑定

Vue 实现数组绑定的方法 Vue 提供了多种方式来实现数组的绑定,以下是常见的几种方法: 使用 v-for 指令绑定数组 通过 v-for 指令可以遍历数组并渲染列表。语法如下: <ul&…

vue如何实现表单绑定

vue如何实现表单绑定

Vue 表单绑定实现方法 Vue 提供了 v-model 指令实现表单元素的双向数据绑定,适用于多种输入类型。 文本输入框绑定 <input type="text" v-model="mess…

vue实现数据双向绑定

vue实现数据双向绑定

Vue 数据双向绑定的实现原理 Vue 的数据双向绑定主要通过 v-model 指令实现,其核心是基于数据劫持和发布-订阅模式。以下是关键实现机制: 数据劫持(响应式原理) Vue 使用 Objec…