Vue3 v-model的使用

简介

使用场景:1 用于表单input、textarea以及select元素上创建双向数据绑定

2 用于组件间数据的通信

一个小栗子,第一个使用场景的代码

javascript 复制代码
<template>
  <div class="box">
    <div class="intro">
      <el-input v-model="inputString" placeholder="Please input"></el-input>
    </div>
    <div class="intro">
      <div>inputvalue最新的值为 = {{ inputString }}</div>
    </div>
  </div>
</template>
<script setup>
import { watch, ref } from 'vue';

const inputString = ref("");
watch(inputString, (newValue, oldValue) => {
  console.log("inputvalue = ", newValue, "oldValue = ", oldValue);
});

</script>
<style lang="less">
.box {
  display: flex;
  flex-direction: column;

  .intro {
    margin: 15px;
    padding: 10px;
  }
}
</style>

v-model可以与一个ref值联用,当input输入框的输入内容改变时,这个ref值会实时的跟着改变

一个小栗子,第二个是用场景:

自定义一个组件A,在组件A中对el-input进行了封装,自定义组件A的代码如下:

javascript 复制代码
<template>
  <div class="box">
    <el-input type="text" v-model="inputStr" @input="inputValueChange"></el-input>
  </div>
</template>
<script setup>
/* vue3.4之后这样使用
import { defineModel } from 'vue';
const inputvalue = defineModel('inputvalue');
*/
import { ref } from 'vue';
defineProps({
  inputvalue: {
    required: true,
    default: "A component value"
  }
})
const emits = defineEmits(['update:inputvalue']);

const inputStr = ref("");

const inputValueChange = () => {
  emits('update:inputvalue',  inputStr.value);
}
</script>
<style scoped lang="less">
</style>

在父组件中使用子组件A,并与父组件中的数据进行绑定,父组件中的代码如下:

javascript 复制代码
<template>
  <div class="box">
    <div class="intro">
      <div>自定义组件与父组件间值的双向绑定:{{ aValue }}</div>
      <A v-model:inputvalue="aValue"></A>
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import A from './components/index.vue';

/*
  也可以用于自定义父子组件间的值的双向绑定
*/
const aValue = ref("super base value");

</script>
<style lang="less">
.box {
  display: flex;
  flex-direction: column;

  .intro {
    margin: 15px;
    padding: 10px;
  }
}
</style>

一个小栗子 ,父组件与子组件之间三个值同时进行双向绑定

创建一个子组件B,代码如下:

javascript 复制代码
<template>
  <div class="intro">
    <div class="btn" @click="oneEvent">change oneVal</div>
    <div class="btn" @click="twoEvent">change twoVal</div>
    <div class="btn" @click="threeEvent">change threeVal</div>
  </div>
</template>
<script setup>
import { ref } from 'vue';
const props= defineProps({
  oneVal: {
    required: true,
    default: ""
  },
  twoVal: {
    required: false,
    default: ""
  },
  threeVal: {
    required: false,
    default: ""
  }
});
const emits = defineEmits(['update:oneVal', 'update:twoVal', 'update:threeVal']);
let numOne = 0;
let numTwo = 0;
let numThree = 0;
/* 
  在点击事件中改变三个与父组件双向绑定的值
*/
const oneEvent = () => {
  numOne = numOne + 2;
  let val = numOne + '';
  console.log("val = ", val);
  emits('update:oneVal', val);
};

const twoEvent = () => {
  numTwo = numTwo + 2;
  let val = numTwo + '';
  console.log("val = ", val);
  emits('update:twoVal', val);
};

const threeEvent = () => {
  numThree = numThree + 2;
  let val = numThree + '';
  console.log("val = ", val);
  emits('update:threeVal', val);
};
</script>
<style lang="less" scoped>
.intro {
  display: flex;
  flex-direction: row;
  margin: 15px;
  padding: 10px;

  .btn {
    margin-right: 150px;
    margin-left: 75px;
    background-color: aquamarine;
    padding: 20px;
  }
}
</style>

父组件中使用这个自定义组件,并与自定义组件进行三个值的双向绑定:

javascript 复制代码
<template>
  <div class="box">
    <div class="intro">
      <B v-model:oneVal="oneVal" v-model:twoVal="twoVal" v-model:threeVal="threeVal"></B>
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import B from './components/b.vue';

const oneVal = ref("父组件初值:oneVal");
const twoVal = ref("父组件初值:twoVal");
const threeVal = ref("父组件初值:threeVal");
</script>
<style lang="less">
.box {
  display: flex;
  flex-direction: column;

  .intro {
    margin: 15px;
    padding: 10px;
  }
}
</style>
相关推荐
110546540128 分钟前
23、电网数据管理与智能分析 - 负载预测模拟 - /能源管理组件/grid-data-smart-analysis
前端·能源
开发者小天28 分钟前
React中startTransition的使用
前端·react.js·c#
@PHARAOH1 小时前
WHAT - 缓存命中 Cache Hit 和缓存未命中 Cache Miss
前端·缓存
计算机学姐2 小时前
基于SpringBoot的小型民营加油站管理系统
java·vue.js·spring boot·后端·mysql·spring·tomcat
Elastic 中国社区官方博客2 小时前
JavaScript 中使用 Elasticsearch 的正确方式,第一部分
大数据·开发语言·javascript·数据库·elasticsearch·搜索引擎·全文检索
万物得其道者成2 小时前
从零开始创建一个 Next.js 项目并实现一个 TodoList 示例
开发语言·javascript·ecmascript
海天胜景2 小时前
无法加载文件 E:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本
前端·npm·node.js
MingT 明天你好!2 小时前
在vs code 中无法运行npm并报无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查
前端·npm·node.js·visual studio code
老兵发新帖2 小时前
pnpm 与 npm 的核心区别
前端·npm·node.js
超级土豆粉2 小时前
怎么打包发布到npm?——从零到一的详细指南
前端·npm·node.js