当前位置:首页 > VUE

vue 如何实现vmodel

2026-02-19 20:56:12VUE

Vue 中实现 v-model 的方法

在 Vue 中,v-model 是一个语法糖,用于在表单元素或自定义组件上实现双向数据绑定。以下是实现 v-model 的几种方式:

在表单元素上使用 v-model

对于原生表单元素(如 inputtextareaselect),v-model 会自动绑定 value 属性并监听 input 事件。

<template>
  <input v-model="message" placeholder="Edit me" />
  <p>Message is: {{ message }}</p>
</template>

<script>
export default {
  data() {
    return {
      message: ''
    }
  }
}
</script>

在自定义组件上使用 v-model

在自定义组件中,v-model 默认会绑定 value 属性和监听 input 事件。可以通过 model 选项或 Vue 2.2+ 的 v-bindv-on 语法自定义。

方法 1:默认实现

vue 如何实现vmodel

<template>
  <custom-input v-model="message"></custom-input>
</template>

<script>
export default {
  data() {
    return {
      message: ''
    }
  },
  components: {
    'custom-input': {
      props: ['value'],
      template: `
        <input
          :value="value"
          @input="$emit('input', $event.target.value)"
        />
      `
    }
  }
}
</script>

方法 2:自定义 prop 和事件 如果需要使用非默认的 valueinput,可以通过 model 选项配置:

<template>
  <custom-input v-model="message"></custom-input>
</template>

<script>
export default {
  data() {
    return {
      message: ''
    }
  },
  components: {
    'custom-input': {
      model: {
        prop: 'text',
        event: 'change'
      },
      props: ['text'],
      template: `
        <input
          :value="text"
          @input="$emit('change', $event.target.value)"
        />
      `
    }
  }
}
</script>

Vue 3 中的 v-model 实现

在 Vue 3 中,v-model 的实现有所变化,支持多个 v-model 绑定,并默认使用 modelValueupdate:modelValue

vue 如何实现vmodel

自定义组件示例:

<template>
  <custom-input v-model="message"></custom-input>
</template>

<script>
export default {
  data() {
    return {
      message: ''
    }
  },
  components: {
    'custom-input': {
      props: ['modelValue'],
      template: `
        <input
          :value="modelValue"
          @input="$emit('update:modelValue', $event.target.value)"
        />
      `
    }
  }
}
</script>

多个 v-model 绑定:

<template>
  <custom-input
    v-model:first="first"
    v-model:second="second"
  ></custom-input>
</template>

<script>
export default {
  data() {
    return {
      first: '',
      second: ''
    }
  },
  components: {
    'custom-input': {
      props: ['first', 'second'],
      template: `
        <input
          :value="first"
          @input="$emit('update:first', $event.target.value)"
        />
        <input
          :value="second"
          @input="$emit('update:second', $event.target.value)"
        />
      `
    }
  }
}
</script>

使用计算属性实现高级绑定

如果需要更复杂的逻辑,可以通过计算属性实现:

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

<script>
export default {
  data() {
    return {
      message: ''
    }
  },
  computed: {
    computedMessage: {
      get() {
        return this.message
      },
      set(value) {
        this.message = value.toUpperCase()
      }
    }
  }
}
</script>

通过以上方法,可以灵活地在 Vue 中实现 v-model 的双向绑定功能。

标签: 如何实现vue
分享给朋友:

相关文章

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…

vue事件实现

vue事件实现

Vue 事件实现方法 在Vue中,事件处理是通过v-on指令或@简写来实现的。以下是几种常见的事件处理方式: 使用v-on指令绑定事件 <button v-on:click="handleC…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

vue实现treetable

vue实现treetable

Vue实现TreeTable的方法 使用第三方组件库(如Element UI) Element UI的el-table组件支持树形表格展示,通过设置row-key和tree-props属性即可实现。…