当前位置: 首页 > news >正文

06 使用v-model实现双向数据绑定

概述

Vue achieves two-way data binding by creating a dedicated directive that watches a data property within your Vue component. The v-model directive triggers data updates when the target data property is modified on the UI.

Vue 通过创建一个专用指令来观察 Vue 组件中的数据属性,从而实现双向数据绑定。当用户界面上的目标数据属性被修改时,v-model 指令就会触发数据更新。

This directive is usually useful for HTML form elements that need to both display the data and modify it reactively – for example, input, textarea, radio buttons, and so on.

该指令通常适用于既需要显示数据又需要对数据进行反应式修改的 HTML 表单元素,例如输入、文本区域、单选按钮等。

We can enable two-way binding by adding the v-model directive to the target element and binding it to our desired data props:

我们可以在目标元素中添加 v-model 指令,并将其绑定到所需的数据道具,从而启用双向绑定:

<template><input v-model="name"/>
</template>
<script>
export default {data() {return {name: ''}}
}
</script>

In the next exercise, we are going to build a component using Vue’s twoway data binding and experiment with what it means to bind data in two ways.

在下一个练习中,我们将使用 Vue 的双向数据绑定构建一个组件,并尝试以两种方式绑定数据的含义。

练习:双向数据绑定

The context for this type of data model is usually forms or wherever you expect both input and output data. By the end of the exercise, we should be able to utilize the v-model attribute in the context of a form.

这类数据模型的上下文通常是表单或任何需要输入和输出数据的地方。在练习结束时,我们应该能够在表单中使用 v-model 属性。

Create a new Vue component file named Exercise1-04.vue in the src/components directory.

在 src/components 目录中新建一个名为 Exercise1-04.vue 的 Vue 组件文件。

在App.vue中引入该组件并渲染:

<script setup>
import Exercise from "./components/Exercise1-04.vue";
</script>
<template><Exercise/>
</template>

Inside Exercise1-04.vue, start by composing an HTML label and bind an input element to the name data prop using v-model inside the template area:

在 Exercise1-04.vue 中,首先创建一个 HTML 标签,然后在模板区域内使用 v-model 将输入元素与名称数据道具绑定:

<div class="form">
<label>Name<input type="text" v-model="name"/>
</label>
</div>

Complete the binding of the text input by returning a reactive data prop called name in the <script> tag:

<script> 标记中返回名为 name 的反应式数据道具,从而完成文本输入的绑定:

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

Next, compose a label and selectable HTML select tied to the language data prop using v-model inside of the template area:

接下来,在模板区域内使用 v 模型编写与语言数据道具绑定的标签和可选 HTML 选项:

<template><div class="form"><label>Name<input type="text" v-model="name"/></label><label>Preferred JavaScript style<select name="language" v-model="language"><option value="Javascript">JavaScript</option><option value="TypeScript">TypeScript</option><option value="CoffeeScript">CoffeeScript</option><option value="Dart">Dart</option></select></label></div>
</template>

Finish binding the select input by returning a reactive data prop called language in the <script> tag:

<script> 标记中返回名为 language 的反应式数据道具,完成选择输入的绑定:

<script>
export default {data() {return {name: '',language: '',}},
}
</script>

Below the form fields, output the name and language inside of an unordered list structure (<ul> and <li>) by using curly braces such as {{ name }}. Your code should look as follows:

在表单字段下方,使用大括号(如 {{ name }})在无序列表结构(<ul><li>)中输出名称和语言。您的代码应如下所示:

<template><section><div class="form"><label>Name<input type="text" v-model="name"/></label><label>Preferred JavaScript style<select name="language" v-model="language"><option value="JavaScript">JavaScript</option><option value="TypeScript">TypeScript</option><option value="CoffeeScript">CoffeeScript</option><option value="Dart">Dart</option></select></label></div><ul class="overview"><li><strong>Overview</strong></li><li>Name: {{ name }}</li><li>Preference: {{ language }}</li></ul></section>
</template>

Add styling inside the <style> tag at the bottom of the component:

在组件底部的"<style>"标签内添加样式:

<style>
.form {display: flex;justify-content: space-evenly;max-width: 800px;padding: 40px 20px;border-radius: 10px;margin: 0 auto;background: #ececec;
}.overview {display: flex;flex-direction: column;justify-content: space-evenly;max-width: 300px;margin: 40px auto;padding: 40px 20px;border-radius: 10px;border: 1px solid #ececec;
}.overview > li {list-style: none;
}.overview > li + li {margin-top: 20px;
}
</style>

When you update the data in the form, it should also update the overview area synchronously.

更新表单中的数据时,也应同步更新概览区域。

最终完整代码如下:

<template><section><div class="form"><label>Name<input type="text" v-model="name"/></label><label>Preferred JavaScript style<select name="language" v-model="language"><option value="JavaScript">JavaScript</option><option value="TypeScript">TypeScript</option><option value="CoffeeScript">CoffeeScript</option><option value="Dart">Dart</option></select></label></div><ul class="overview"><li><strong>Overview</strong></li><li>Name: {{ name }}</li><li>Preference: {{ language }}</li></ul></section>
</template>
<script>
export default {data() {return {name: '',language: '',}},
}
</script>
<style>
.form {display: flex;justify-content: space-evenly;max-width: 800px;padding: 40px 20px;border-radius: 10px;margin: 0 auto;background: #ececec;
}.overview {display: flex;flex-direction: column;justify-content: space-evenly;max-width: 300px;margin: 40px auto;padding: 40px 20px;border-radius: 10px;border: 1px solid #ececec;
}.overview > li {list-style: none;
}.overview > li + li {margin-top: 20px;
}
</style>

In this exercise, we used the v-model directive to bind the name and JavaScript-style drop-down selection to our local state’s data. When you modify the data, it will reactively update the DOM elements to which we output its value.

在本练习中,我们使用 v-model 指令将名称和 JavaScript 样式的下拉选择绑定到本地状态数据。当您修改数据时,它将反应式地更新我们输出其值的 DOM 元素。

Next, we will discuss our v-for directive further and different approaches to handling iterative data collection in Vue.

接下来,我们将进一步讨论 v-for 指令以及在 Vue 中处理迭代数据收集的不同方法。

http://www.lryc.cn/news/261572.html

相关文章:

  • 【C++11特性篇】C++11中新增的initializer_list——初始化的小利器(2)
  • 计算机网络传输层(期末、考研)
  • 【STM32入门】4.1中断基本知识
  • HCIA-H12-811题目解析(3)
  • 【异步绘制】UIView刷新原理 与 异步绘制
  • [ERROR] ocp-server-ce-py_script_start_check-4.2.1 RuntimeError: ‘tenant_name‘
  • 模拟实验中经常遇到的问题和常用技巧
  • 微信小程序(二) ——模版语法1
  • 牛客小白月赛83 解题报告
  • 蓝桥杯专题-真题版含答案-【三角螺旋阵】【干支记年法】【异或加密法】【金字塔】
  • 鸿蒙篇——初次使用鸿蒙原生编译器DevEcoStudio创建一个鸿蒙原生应用遇到的坑--汇总(持续更新)
  • 细胞培养之一二三:哺乳动物细胞培养污染问题和解决方案
  • 《Linux C编程实战》笔记:文件属性操作函数
  • linux中的网络知识
  • tp中的调试模式
  • 【docker 】基于Dockerfile创建镜像
  • C# 提取PDF中指定文本、图片的坐标
  • CTF网络安全大赛是干什么的?发展史、赛制、赛程介绍,参赛需要学什么?
  • 阿里云SMC迁移RedHat/CentOS 5 内核升级
  • 无代码开发让合利宝支付与CRM无缝API集成,提升电商用户运营效率
  • 数据标注公司如何确保数据安全?景联文科技多维度提供保障
  • (C语言)精确计算程序运行时间的方法
  • 【Vulnhub 靶场】【VulnCMS: 1】【简单】【20210613】
  • 普冉(PUYA)单片机开发笔记(10): I2C通信-配置从机
  • Idea maven打包时 报错 illegalArgumentException: Malformed \uxxxx encoding 解决方法
  • Qt中槽函数在那个线程执行的探索和思考
  • C++ 类模板
  • 边缘计算系统设计与实践
  • 【Spark精讲】Spark存储原理
  • 贪心算法:买卖股票的最佳时机II 跳跃游戏 跳跃游戏II