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

子组件代码:

<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 props实现双向绑定

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

相关文章

vue双向绑定实现简单

vue双向绑定实现简单

Vue 双向绑定的简单实现 双向绑定的核心是通过数据劫持结合发布-订阅模式实现。以下是简化版的实现步骤: 数据劫持(Object.defineProperty) 通过 Object.defineP…

react如何绑定事件

react如何绑定事件

React 事件绑定方法 在 React 中绑定事件有多种方式,以下是常见的几种方法: 使用 onClick 或其他事件属性直接绑定 <button onClick={() => co…

vue实现双向滚动

vue实现双向滚动

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

vue实现数据双向

vue实现数据双向

在Vue中实现数据双向绑定主要依靠v-model指令,它结合了属性绑定和事件监听,适用于表单元素或自定义组件。以下是具体实现方式: 基础表单元素的双向绑定 对于原生表单元素(如input、text…

vue双向绑定实现

vue双向绑定实现

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

vue双向绑定实现方式

vue双向绑定实现方式

Vue 双向绑定的实现方式 Vue 的双向绑定主要通过 v-model 指令实现,结合数据劫持和发布-订阅模式完成数据与视图的同步更新。以下是核心实现原理及方式: 数据劫持(响应式原理) Vue…