Home>Article>Web Front-end> Vue.js Learning 2: Data-Driven Development
Vue.js Tutorialcolumn introduces the data-driven development of Vue.js learning part two.
In the Vue.js framework, the way to interact with HTML page elements is not as direct as the native JavaScript interface. It is first embedded in a series of similar HTML element tags. Bind data to the Vue directive attributes of ordinary tag attributes, and then modify the display mode and content of page elements by modifying these bound data in JavaScript code. In terms of programming methods, we usually call this way of using changes in data content to drive the business operations of the entire program "Data-driven development". This part of the notes will record how to use data-driven development methods to complete data binding and event response to achieve basic functions such as controlling page elements and CSS styles.
In the previous01_sayHello
program, we now bind a name using template syntax in the4a249f0d628e2318394fd9b75b4636b1
tag For the data ofsayHello
, this template syntax is actually the syntactic sugar of thev-text
instruction. In other words, the more standardized syntax of the4a249f0d628e2318394fd9b75b4636b1
tag should be:
Considering our traditional habit of writing HTML tags, use{{ data_name }}
Such template tags will be more comfortable. Of course, thev-text
directive sets the text content under the current element label. If you want to bind data to the attribute of the label itself, you have to use thev-bind
directive. The specific syntax is to add thev-bind:
prefix in front of the label attribute name to be set. For example, if you want to bind data to thesrc
attribute of thea1f02c36ba31691bcfe87b2722de723b
tag, you can do this:
Also,v-bind# The ## directive also has an abbreviated form. You only need to add a
:prefix before the label attribute name to which data is bound. In the previous
01_sayHelloprogram, I used this form. After binding the data on the page element, you can create a Vue instance in the corresponding JavaScript script. This is the
main.jsfile I used in the
01_sayHelloprogram. content written in. At least the following two members must be defined in this Vue instance object:
Member
: This member is used to set the element container corresponding to the current Vue instance. , which is usually ae388a4556c0f65e1904146cc1a846beeelement. In some cases, it can also be the
1aa9e5d373740b65a0cc8f0a02150c53,
c37f8231a37e88427e62669260f0074dand other container classes provided by HTML5. Label. The value of this member can be any string that conforms to CSS selector syntax, such as
#ID,
.CLASSNAME, etc.
Member
: This member is used to set the data bound in the page element. Its value itself is also an object in JSON format. Each of the objects Each member corresponds to an object bound in a page element. For example, in the previous01_sayHelloprogram, I bound the two data
sayHelloand
vueLogo, You must set corresponding values for them in the
datamember of the Vue object.
01_sayHelloprogram is currently only a one-way data display business. If you want to make it interactive, you also need to bind events to page elements. In the Vue.js framework, to bind an event, you must first register an event handler for the target element tag through the
v-ondirective. For example, if we want to use a button in the
01_sayHelloprogram To control whether the
a1f02c36ba31691bcfe87b2722de723belement is displayed or not, you can modify the
index.htmcode of the program as follows:
Here, I mainly did the following Modification:你好,Vue.js {{ sayHello }}
directive is added to the
a1f02c36ba31691bcfe87b2722de723btag, which can be used to bind a Boolean type Data (that is,
isShowhere) to determine whether to display the label where it is located.
directive is used to set the
valueattribute of the label. The value of this attribute is a condition An expression that will display different text based on the value of
isShow.
command (
@is the abbreviation of the
v-on:prefix of the command) for the new button The label registers a click event handler named
toggleShow.
main.js, as follows:
const app = new Vue({ el: '#app', data:{ sayHello: '你好,Vue.js!', vueLogo: 'img/logo.png', isShow: true }, methods:{ toggleShow: function() { this.isShow = !this.isShow; } } });Here, I mainly made the following modifications:
data
成员中定义了之前绑定的布尔类型数据isShow
,并将其默认值设置为 true。methods
的成员。该成员用于设置页面元素中注册的事件处理函数,它的值也是一个 JSON 格式的对象。该对象的每个成员都对应一个已在页面元素中用v-on
指令注册的事件处理函数。在这里就是toggleShow
,该函数每次调用都会将isShow
的值取反。这样一来,当我们在 Web 浏览器中打开该应用程序就会看到之前的 Vue 图标旁边多了个文本内容为隐藏
的按钮。当按钮被单击之后,图标就会消失,按钮上的文本也变成显示。之后,如果该按钮再次被单击,一切又会恢复原状。
对于用户来说,除了触发事件之外,允许用户在运行时输入具体的数据也是一个应用程序必不可少的一项功能。下面将通过编写一个"待办事项"的程序来学习如何使用 Vue.js 框架实现能处理用户输入数据的应用程序界面。为此,我需要在code
目录下创建一个名为02_toDoList
的目录,并在该目录中创建一个名为index.htm
的文件,其代码如下:
待办事项
待办事项
接下来,我会在同一目录下再创建一个名为js
的目录,并在该目录下创建main.js
脚本文件,其代码如下:
// 程序名称: toDoList // 实现目标: // 1. 学习 v-model、v-for 等指令 // 2. 掌握如何处理用户输入 const app = new Vue({ el: '#app', data:{ newTask: '', taskList: [], doneList: [] }, methods:{ addNew: function() { if(this.newTask !== '') { this.taskList.push(this.newTask); this.newTask = ''; } }, remove: function(index) { if(index >= 0) { this.taskList.splice(index,1); } } } });
下面来具体分析一下这个程序。在通常情况下,Web 应用程序在前端接受用户输入信息的方式主要有两种:第一种方式是用文本框元素来获取用户从键盘输入的信息。在 Vue.js 框架中,我们可以用v-model
指令将e67338802192d7bd5294b2aa965c9bc5
标签绑定到newTask
数据中,该指令与v-bind
指令不同的在于,它是一种双向绑定。也就是说,v-model
指令不止可以让 JavaScript 代码中对绑定数据的修改反映到页面元素中,也可以让页面元素获取到的用户输入数据同步到 JavaScript 代码中。在 JavaScript 代码获取到文本框中的用户输入内容之后,我们就可以通过事件处理函数addNew
将其加入到一个名为taskList
的数据列表中,然后将该事件处理函数注册给输入回车和鼠标单击事件。
第二种方式是用单选钮、复选框等选择性元素来获取用户用鼠标勾选的信息。例如在上面这个程序中,我在5ab0e995a190de047e013a6966610e55
元素中用v-for
指令创建了一系列514d05be645eb7d51e331036aaf6fa36
,它们都提供v-model
指令将自己双向绑定到了另一个名为doneList
数据列表上。在 Vue.js 框架中,如果一组复选框元素被v-model
指令绑定到了一个列表类型的数据上,那么当这些复选框被选上时,它们的value
属性值就会被自动添加到这个被绑定的数据列表中。
为了证明被选中的复选框被加入到了doneList
数据列表中,我还在页面中添加了一个e65cf9c0646c5567cdbd377fa7d191fd
元素,并对doneList
数据列表进行了遍历显示。需要说明的是,用于遍历数据列表的v-for
指令主要有两种形式:
v-for="item in dataList"
, then the data list (Each item of data in dataList
) will be read one by one by the iterative variable (item
) during the traversal process.v-for="(item,index) in dataList"
, at this time, during the traversal process of the data list (dataList
), the value of each item of data will be read by theitem
variable, and the index of each item of data will beindex
Variable reading.The last thing to note is that for the9359e0ff49de62abac442c8fefd08430
element itself, I used thev-if
directive, which The effect is basically the same as the previousv-show
instruction. The only difference is: thev-if
instruction will directly add or delete the element node where it is located on the DOM tree, while # The ##v-showcommand simply hides or displays the element through the
styleattribute of the element where it is located. In terms of execution efficiency, the
v-showcommand is more efficient. Here, we set that when there is no data in the
doneListlist, the program will directly remove the
9359e0ff49de62abac442c8fefd08430element from the page, and vice versa. Add this element to the page. Let’s take a look at the effect of
02_toDoListprogram operation:
## More related free learning:javascript(Video)
The above is the detailed content of Vue.js Learning 2: Data-Driven Development. For more information, please follow other related articles on the PHP Chinese website!