This time I will show you how to operate vue.js data binding, and what are the precautions for how to operate vue.js data binding. The following is a practical case, let's take a look.
The example in this article describes the vue.js data binding operation. Share it with everyone for your reference, the details are as follows:
Data Binding
Responsive data binding system. After the binding is established, the DOM will be synchronized with the data, and there is no need to manually maintain the DOM. Make the code more concise and easy to understand and improve efficiency.
Data binding syntax
1. Text interpolation
{{ }}Mustache tag
<span>Hello {{ name }}</span>
data:{ name: 'vue' } == > Hello vue
Single interpolation
Changing the vm instance attribute value after the first assignment will not cause DOM changes
<span>{{ name }}</span>
2. HTML attributes
Mustache tag {{ }}
<p></p>
Abbreviation:
<p></p>
3. Binding expression
The text content placed in the Mustache tag. In addition to directly outputting the attribute value, a binding expression can consist of a simple JavaScript expression and optionally one or more filters (regular expressions are not supported. If complex conversion is required, use filters or computed properties for processing).
{{ index + 1}} {{ index == 0 ? 'a' : 'b' }} {{name.split('').join('|') }} {{ var a = 1 }} //无效
4. Filter
vue.js allows adding optional filters after expressions, indicated by the pipe character "|".
{{ name | uppercase }} // Vue.js将name的值传入给uppercase这个内置的过滤器中(本质是一个函数),返回字符串的大写值。 {{ name | filterA | filterB }} //多个过滤器链式使用 {{ name | filterA arg1 arg2 }} //传入多个参数
At this time, filterA passes the value of name as the first parameter, and arg1 and arg2 as the second and third parameters into the filter function.
The return value of the final function is the output result. arg1 and arg2 can use expressions or add single quotes to directly pass in strings.
For example:
{{ name.split('') | limitBy 3 1 }} // ->u,e
The filter limitBy can accept two parameters. The first parameter is to set the number of displays. The second parameter is optional and refers to the array subscript of the starting element. .
The 10 built-in filters of vue.js (removed in Vue.js2.0)
capitalize: The first character of the string is converted to uppercase.
uppercase: Convert the string to uppercase.
lowercase: The string is converted to lowercase.
currency: The parameters are {String}[currency symbol], {Number}[decimal places], convert the number into currency symbol, and automatically add numerical section numbers.
{{ amount | currency '¥' 2 }} //若amount值为1000,则输出为¥1,000.00
pluralize: The parameters are {String}single,[double,triple], and the string is pluralized.
<p>{{ c | pluralize 'item' }} {{ c | pliralize 'st' 'nd' 'rd' 'th' }} </p>
//输出结果: 1item 1st 2items 2nd 3items 3rd 4items 4th
json: The parameter is {Number}[indent] space indentation number, and the json object data is output into a string that conforms to the json format.
debounce: The incoming value must be a function, and the parameter is optional, which is {Number}[wait], which is the delay length. The effect is that the action will not be executed until n milliseconds after the function is called.
<input> //input元素上监听了keyup事件,并且延迟500ms触发
limitBy: The incoming value must be an array, the parameter is {Number}limit
, {Number}[offset]
, and the limit is Display the number, offset is the starting array subscript.
<p></p> //items为数组,且只显示数组中的前十个元素
filterBy: The incoming value must be an array, and the parameter is {String | Function}targetStringOrFunction
, which is the string or function that needs to be matched; "in" can Select separator. {String}[...searchKeys]
, is the retrieved attribute area.
<p>{{ name }}</p> //检索names数组中值包含1.0的元素 <p>{{ item | json }}</p> //检索items中元素属性name值为1.0的元素输出。检索区域也可以为数组,即in [name,version],在多个属性中进行检索。
//输出结果 vue1.0 {"name":"vue1.0","version":"1.0"}
Use a custom filter function, which can be defined in the options methods
<p></p>orderBy: The incoming value must be an array, and the parameter is
{String |Array|Function}sortKeys
, which specifies the sorting strategy.Single key name:
{{ item.name}}
//items数组中以键名name进行降序排列Multiple key names:
<p>{{item.name}}</p> //使用items里的两个键名进行排序Custom sorting function:
<p>{{item.name}}</p> methods:{ customOrder: function(a,b){ return parseFloat(a.version) > parseFloat(b.version) //对比item中version的值的大小进行排序 } }5. Command The value of the
directive is limited to the binding expression, that is, when the value of the expression changes, some special behavior will be applied to the bound DOM.
Parameter: src is the parameter
<img src="/static/imghwm/default1.png" data-src="avatar" class="lazy" alt="How to operate vue.js data binding" > <img src="/static/imghwm/default1.png" data-src="{{avatar}}" class="lazy" alt="How to operate vue.js data binding" >Modifier: a special suffix starting with a half-width period., used to indicate that the instruction should be bound in a special way.
<button></button> //stop:停止冒泡。相当于调用了e.stopPropagagation().计算属性
避免在模板中加入过重的业务逻辑,保证模版的结构清晰和可维护性。
1.基础例子
var vm = new Vue({ el: '#app', data: { firstName:'Gavin', lastName:'CLY' }, computed: { fullName:function(){ //this指向vm实例 return this.firstName + ' ' + this.lastName; } } })<p>{{ firstName }}</p> //Gavin <p>{{ lastName }}</p> //CLY <p>{{ fullName }}</p> //Gavin CLY注:此时对
vm.firstName
和vm.lastName
进行修改,始终会影响vm.fullName
。2.Setter
更新属性时带来便利
var vm = new Vue({ el:'#el', data:{ cents:100 }, computed:{ price:{ set:function(newValue) { this.cents = newValue * 100; }, get:function(){ return (this.cents / 100).toFixed(2); } } } })表单控件
v-model:对表单元素进行双向数据绑定,在修改表单元素值时,实例vm中对应的属性值也同时更新,反之亦然。
var vm = new Vue({ el:'#app', data: { message: '', gender: '', cheched: '', multiChecked: '', a: 'checked', b: 'checked' } })1. Text
输入框示例,用户输入的内容和vm.message直接绑定:
<input> <span>Your input is : {{ message }} </span>2. Radio
单选框示例:
<label><input>男 <label><input>女 <p>{{ gender }}</p> //显示的是value值</label></label>3.Checkbox
单个勾选框,v-model即为布尔值,此时Input的value并不影响v-model的值。
<input> <span>checked: {{ checked }} </span> //显示的是true/false多个勾选框,v-model使用相同的属性名称,且属性为数组。
<label><input>1</label> <label><input>2</label> <label><input>3</label> <p>MultiChecked:{{ multiChecked.join{'|'} }}</p> //multiChecked:1|24.Select
单选
<select> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span>多选
<select> <option>A</option> <option>B</option> <option>C</option> </select> <span>MultiSelected: {{ multiSelected.join('|') }}</span>5.绑定value
通过v-bind实现,表单控件的值绑定到Vue市里的动态属性上。
Checkbox
<input>选中:
vm.checked == vm.a //=> true未选中:
vm.checked == vm.b //=>trueRadio
<input>选中:
vm.checked == vm.a //=> true3.Select Options
<select> <!-- 对象字面量 --> <option>123</option> </select>选中:
typeof vm.selected //=> object vm.selected.number //=> 1236.参数特性
.lazy:默认情况下,v-model在input事件中同步输入框与数据,加lazy属性后会在change事件中同步。
<input>.number:自动将用户输入转为Number类型,如果原值转换结果为NaN,则返回原值。
<input>.trim:如果要自动过滤用户输入的首尾空格,可以添加 trim 修饰符到 v-model 上过滤输入
<input>Class与Style绑定
1.Class绑定
对象语法:
v-bind:class
接受参数是一个对象,而且可以与普通的class属性共存。<p></p>vm实例中需要包含:
data:{ active:true }渲染结果为:
<p></p>数组语法:
v-bind:class
也接受数组作为参数。<p></p>vm实例中需要包括:
data:{ classA:"class-a", classB:"class-b" }渲染结果为:
<p></p>使用三元表达式切换数组中的class
<p></p>若
vm.isB = false则渲染结果为
<p></p>2.内联样式绑定(style属性绑定)
对象语法:直接绑定符合样式格式的对象。
<p></p>vm实例中包含:
data:{ alertStyle:{ color: 'red', fontSize: '2px' } }<p></p>数组语法:
v-bind:style
允许将多个样式对象绑定到同一元素上。<p></p>3.自动添加前缀
在使用transform这类属性时,
v-bind:style
会根据需要自动添加厂商前缀。:style
在运行时进行前缀探测,如果浏览器版本本省就不支持不加前缀的css属性,那就不会添加。相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
VUE做出带有数据收集、校验和提交功能表单
The above is the detailed content of How to operate vue.js data binding. For more information, please follow other related articles on the PHP Chinese website!

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

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

SublimeText3 Linux new version
SublimeText3 Linux latest version