This time I will show you how to operate vue.js data binding, and what are the precautions for operating vue.js data binding. The following is a practical case, let's take a look.
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
Hello {{ name }}
Copy after login
data:{ name: 'vue' } == > Hello vue
Copy after login
Single interpolation
Changing the vm instance attribute value after the first assignment will not cause DOM changes
{{ name }}
Copy after login
2. HTML attributes
Mustache tag {{ }}
Copy after login
Abbreviation:
Copy after login
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 }} //无效
Copy after login
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 }} //传入多个参数
Copy after login
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
Copy after login
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.
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元素上监听了keyup事件,并且延迟500ms触发
Copy after login
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.
//items为数组,且只显示数组中的前十个元素
Copy after login
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.
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
<==>
Copy after login
Modifier: a special suffix starting with a half-width period., used to indicate that the instruction should be bound in a special way.
//stop:停止冒泡。相当于调用了e.stopPropagagation().
Copy after login
Computed Properties
避免在模板中加入过重的业务逻辑,保证模版的结构清晰和可维护性。
1.基础例子
var vm = new Vue({ el: '#app', data: { firstName:'Gavin', lastName:'CLY' }, computed: { fullName:function(){ //this指向vm实例 return this.firstName + ' ' + this.lastName; } } })
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!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn