A detailed introduction to common template syntax in Vue.js
This article mainly introduces the detailed explanation of the common template syntax of Vue.js study notes. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
This article introduces the common template syntax of Vue.js and shares it with everyone. The details are as follows:
1. Text rendering
Vue supports dynamic rendering of text, that is, rendering text content in real time while modifying properties. At the same time, in order to improve rendering efficiency, it is also supported to render only once, that is, after the text is rendered for the first time, the text content will no longer change with the change of the attribute value.
Real-time rendering
1 2 3 4 5 |
|
The v-model instruction dynamically binds the value of the input tag to the attribute msg. Displayed on the page via {{ msg }} expression. When the content of the text box is modified, the content of the subsequent pages will change in real time and remain consistent with the content of the text box.
One rendering
1 2 3 4 5 |
|
Add attributes to the vm object
1 2 3 4 5 6 7 8 |
|
When the page is loaded for the first time, the page displays once.... When the span tag is added with the v-once instruction, the page content will not change no matter how the text box content is modified. .
HTML code rendering
In some cases, the page needs to dynamically insert a piece of HTML code
Add attributes to the vm object, the attribute value is A piece of HTML code
1 2 3 4 5 6 7 8 |
|
After adding the v-html
directive to the page element, the corresponding element
##
1 2 3 4 |
|
2. Expression
Already introduced above {{ msg }} is a simple expression. In addition, there are some commonly used expressions.
Operation expression
Operators can be used in the above simple expressions, and Vue will render the result after the operation on the page1 2 3 4 5 6 |
|
Ternary operation expression
Vue supports the use of ternary operator within expressions1 2 3 4 |
|
Javascript method
Basic methods supported by Javascript can also be used in expressions. Vue will dynamically execute the method and display the final result1 2 3 4 |
|
3. Filter
Filters are often used to format and display content. Vue supports custom filtersFirst, add filter methods in the vm object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
filters attribute, the first parameter value of the method is the original content passed in that needs to be processed, and the return value of the method is the new content that has been processed.
1 2 3 4 |
|
filter_msg attribute value is passed through the
filter1 filter, and the content is appended and displayed.
1 2 3 4 |
|
1 2 3 4 |
|
四、常用指令
指令就是将一些特殊行为应用到页面DOM元素的特殊属性。Vue的内置指令都是一些带有 v- 前缀的特殊属性。
常用的指令如下:
v-bind
v-on
v-for
v-if
v-else-if
v-else
v-show
v-bind
该指令用来给元素绑定一个或多个特性
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 |
|
在上面的例子里,给 vm 对象增加一个名称为 btn_enabled 的布尔属性,在按钮中通过 v-bind:disabled="btn_enabled" 动态给 disabled 属性赋值
页面上可以看到“不能点击的按钮”动态增加了一个 disabled 属性。同时 v-bind:disabled="!btn_enabled" 也可以简写成 :disabled="!btn_enabled" 。
v-on
该指令绑定事件监听器。事件类型由参数指定。表达式可以是一个方法的名字或一个内联语句。用在普通元素上时,只能监听原生 DOM 事件。在监听原生 DOM 事件时,方法以事件为唯一的参数。如果使用内联语句,语句可以访问一个 $event
属性。
在 vm
对象的 methods
属性里添加自定义方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
通过属性 v-on:click="btn_click"
在按钮上添加 click
事件
1 2 3 4 5 6 7 |
|
页面效果如下
v-on指令也支持缩写,用@符号代替,比如: @click="btn_click"
。
v-for
该指令用来基于源数据多次渲染元素或模板块。
在 vm 对象里添加一个数组类型的属性 books
1 2 3 4 5 6 7 8 |
|
通过 v-for 指令实现一个简单列表
1 2 3 4 5 6 7 8 |
|
页面效果如下
v-for 属性值是一个 item in expression
结构的表达式,其中 item
代表 expression
运算结果的每一项。最终的HTML代码如下
v-if、v-else-if、v-else
条件渲染指令,通过表达式结果的真假来插入和删除元素。 v-if 可以单独使用,而 v-else-if 、 v-else 必须和 v-if 一起使用。
下面是一个简单用法的例子
1 2 3 4 5 6 7 8 |
|
页面显示如下
当文本框里输入小于10的数字时,右侧只显示“小于10”的文本内容。
查看源码发现,只有“小于10”的内容的 span 标签存在,另外两个标签被插入到页面中。
同样,在输入大于10的数字时,右侧只显示“大于10”的文本内容。而源码里只有对应的 span 标签存在。
v-show
该指令也是条件渲染指令,但是与上述的 v-if 有不同。这可以通过一个例子来说明。
1 2 3 4 5 6 7 8 |
|
将上面例子里的指令都换成 v-show ,页面显示如下
从页面显示的结果上看,没有任何区别。但是查看源码会发现,符合表达式结果判断的元素内容将被显示,不符合结果判断的元素将被隐藏,即被加上 display: none; 的样式。
It can be seen from the comparison of the above two examples
v-if: dynamically insert or delete elements
v-show: Dynamically show or hide elements
The above is the detailed content of A detailed introduction to common template syntax in Vue.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

Vue.js is not difficult to learn, especially for developers with a JavaScript foundation. 1) Its progressive design and responsive system simplify the development process. 2) Component-based development makes code management more efficient. 3) The usage examples show basic and advanced usage. 4) Common errors can be debugged through VueDevtools. 5) Performance optimization and best practices, such as using v-if/v-show and key attributes, can improve application efficiency.

Vue.js is mainly used for front-end development. 1) It is a lightweight and flexible JavaScript framework focused on building user interfaces and single-page applications. 2) The core of Vue.js is its responsive data system, and the view is automatically updated when the data changes. 3) It supports component development, and the UI can be split into independent and reusable components.

The connection and difference between Go language and JS Go language (also known as Golang) and JavaScript (JS) are currently popular programming languages. They are related in some aspects and have obvious differences in other aspects. This article will explore the connections and differences between the Go language and JavaScript, and provide specific code examples to help readers better understand these two programming languages. Connection: Both Go language and JavaScript are cross-platform and can run on different operating systems.

Overview of how to use WebSocket and JavaScript to implement an online electronic signature system: With the advent of the digital age, electronic signatures are widely used in various industries to replace traditional paper signatures. As a full-duplex communication protocol, WebSocket can perform real-time two-way data transmission with the server. Combined with JavaScript, an online electronic signature system can be implemented. This article will introduce how to use WebSocket and JavaScript to develop a simple online
