Vue3中的v-model

1、v-model

Vue2

Vue2中得 v-model 默认解析成 :value 与 @input

原理实现

:

html 复制代码
<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld :value="count" @input="count = $event"></HelloWorld>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  data() {
    return { count: 0 };
  }
};
</script>

<style></style>

:

html 复制代码
<template>
  <div>
    {{ value }}
    <button @click="$emit('input', value + 1)">+1</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    value: Number,
  },
};
</script>
<style scoped></style>

语法糖实现

:

html 复制代码
<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld v-model="count" />
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  data() {
    return { count: 0 };
  }
};
</script>

<style></style>

:

html 复制代码
<template>
  <div>
    {{ value }}
    <button @click="$emit('input', value + 1)">+1</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    value: Number,
  },
};
</script>
<style scoped></style>

Vue3

Vue3中得 v-model 默认解析成 :modelValue 与 @update:modelValue

原理实现

:

html 复制代码
<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld
      :modelValue="count"
      @update:modelValue="count = $event"
    ></HelloWorld>
  </div>
</template>

<style lang="scss" scoped></style>

:

html 复制代码
<script setup lang="ts">
defineProps<{
  modelValue: number
}>()
defineEmits<{
  (e: 'update:modelValue', newCount: number): void
}>()
</script>

<template>
  <div>
    {{ modelValue }}
    <button @click="$emit('update:modelValue', modelValue + 1)">+1</button>
  </div>
</template>

<style lang="scss" scoped></style>

语法糖实现

html 复制代码
<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld v-model="count"></HelloWorld>
  </div>
</template>

<style lang="scss" scoped></style>

:

html 复制代码
<script setup lang="ts">
defineProps<{
  modelValue: number
}>()
defineEmits<{
  (e: 'update:modelValue', newCount: number): void
}>()
</script>

<template>
  <div>
    {{ modelValue }}
    <button @click="$emit('update:modelValue', modelValue + 1)">+1</button>
  </div>
</template>

<style lang="scss" scoped></style>

2、 .sync

Vue2

Vue2中得 :attr.sync 默认解析成 :attr 与 @update:attr

原理实现

html 复制代码
<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld :count="count" @setCount="count = $event" />
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  data() {
    return { count: 0 };
  }
};
</script>

<style></style>

html 复制代码
<template>
  <div class="hello">
    {{ count }}
    <button @click="$emit('setCount', count + 1)">+1</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    count: Number,
  },
};
</script>
<style scoped></style>

语法糖实现

html 复制代码
<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld :count.sync="count" />
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
  },
  data() {
    return { count: 0 };
  }
};
</script>

<style></style>

html 复制代码
<template>
  <div class="hello">
    {{ count }}
    <button @click="$emit('update:count', count + 1)">+1</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    count: Number,
  },
};
</script>
<style scoped></style>

Vue3

Vue3中得 v-model:attr 默认解析成 :attr 与 @update:attr

原理实现

:

html 复制代码
<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld :count="count" @setCount="count = $event"></HelloWorld>
  </div>
</template>

<style lang="scss" scoped></style>

:

html 复制代码
<script setup lang="ts">
defineProps<{
  count: number
}>()
defineEmits<{
  (e: 'setCount', newCount: number): void
}>()
</script>

<template>
  <div>
    {{ count }}
    <button @click="$emit('setCount', count + 1)">+1</button>
  </div>
</template>

<style lang="scss" scoped></style>

语法糖实现

:

html 复制代码
<script setup lang="ts">
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <div>
    {{ count }}
    <button @click="count++">+1</button>
    <hr />
    <HelloWorld v-model:count="count"></HelloWorld>
  </div>
</template>

<style lang="scss" scoped></style>

:

html 复制代码
<script setup lang="ts">
defineProps<{
  count: number
}>()
defineEmits<{
  (e: 'update:count', newCount: number): void
}>()
</script>

<template>
  <div>
    {{ count }}
    <button @click="$emit('update:count', count + 1)">+1</button>
  </div>
</template>

<style lang="scss" scoped></style>

3、总结

Vue2中得 v-model 默认解析成 :value 与 @input

Vue3中得 v-model 默认解析成 :modelValue 与 @update:modelValue


Vue2中得 :attr.sync 默认解析成 :attr 与 @update:attr

Vue3中得 v-model:attr 默认解析成 :attr 与 @update:attr


作用:用于在自定义组件中实现父子组件之间的双向数据绑定

也就是说在Vue3中只有v-model,没有.sync

如果父组件只写v-model="xxx",子组件中接收到的props为modelValue ,抛出的自定义事件为update:modelValue

如果父组件写v-model:attr="xxx" (attr为自定义变量),子组件中接收到的props为attr ,抛出的自定义事件为update:attr

即:Vue3中使用双向数据绑定v-model时props不局限于只能接收modelValue,可以自定义

使用方式:

ts 复制代码
父: 
<HelloWorld v-model="count"></HelloWorld>

子:
defineProps<{
  modelValue: number
}>()
defineEmits<{
  (e: 'update:modelValue', newCount: number): void
}>()
$emit('update:modelValue', modelValue + 1)"

=====================================================================================

父:
<HelloWorld v-model:count="count"></HelloWorld>

子:
defineProps<{
  count: number
}>()
defineEmits<{
  (e: 'update:count', newCount: number): void
}>()
$emit('update:count', count + 1)">
相关推荐
大橙子额23 分钟前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
WooaiJava2 小时前
AI 智能助手项目面试技术要点总结(前端部分)
javascript·大模型·html5
LYFlied2 小时前
从 Vue 到 React,再到 React Native:资深前端开发者的平滑过渡指南
vue.js·react native·react.js
爱喝白开水a2 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
Never_Satisfied2 小时前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
董世昌412 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
B站_计算机毕业设计之家2 小时前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法
WeiXiao_Hyy3 小时前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
吃杠碰小鸡3 小时前
高中数学-数列-导数证明
前端·数学·算法
kingwebo'sZone3 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word