Home>Article>Web Front-end> Vue computed properties and listeners and filters super detailed introduction
This article brings you relevant knowledge aboutvue, which mainly introduces detailed introduction to calculated properties, listeners and filters, including the total use of calculated properties in the shopping cart, etc. Let’s take a look at the content below. I hope it will be helpful to everyone.
【Related recommendations:javascript video tutorial,vue.js tutorial】
Overview:
Putting too much logic in a template will make the template overweight and difficult to Maintenance, using calculated properties can make templates concise and easy to maintain. Computed properties are cached based on their responsive dependencies. Computed properties are more suitable for processing multiple variables or objects and returning a result value. That is, if one of the values of multiple variables changes, the value we monitor will be The value will also change.
Computed properties are defined in the Vue object. Functions are defined in the keyword computed property object and return a value. When using calculated properties, they are used in the same way as the data in data.
Usage:
When we do not use calculated properties, we want to calculate the results in the template, and there are several ways to write them:
{{ n1+n2 }}
{{ sum() }}
{{ sum() }}
{{ sum() }}
If you want to calculate a result, you can use vue to provide calculated properties, and the calculated properties also have a caching function. If your dependencies have not changed, it will read the data in the cache when it is called again.
{{total}}
{{total}}
{{total}}
Note:
If the defined calculated property is abbreviated, an error will be reported when assigning a value to the calculated property. Only when written in a standard way, it can perform assignment operations on calculated properties. Let's take a look at what the standard writing method is.
{{ sum() }}
{{msg}}
Note:
vue学习使用
序号 | 名称 | 单价 | 数量 | 操作 |
---|---|---|---|---|
{{index+1}} | {{item.name}} | {{item.price}} |
Overview:
Use watch to listen for changes in data in data. The attributes in watch must be data that already exists in data.
When you need to monitor changes in an object, the ordinary watch method cannot monitor the changes in the internal properties of the object. Only the data in data can monitor the changes. At this time, you need the deep attribute to monitor the object in depth. .
Usage:
Standard writing:
{{errorUsername}}
Note:
Abbreviation:
{{errorUsername}}
When initializing, enable the listener writing method:
{{errorUsername}}
注意:这个配置只有在标准写法下才能有。
监听对象中的属性变化:
监听对象变化:
注意:
概述:
在数据被渲染之前,可以对其进行进一步处理,比如将字符截取或者将小写统一转换为大写等等,过滤器本身就是一个方法。
过滤器的作用就是为了对于界面中显示的数据进行处理操作。
过滤器可以定义全局或局部。
定义全局过滤器:
{{ phone | phoneCrypt }}
上面的全局过滤器的回调函数中只有一个参数,我们还可以定义多个参数:
{{ phone | phoneCrypt('!!!!') }}
定义局部过滤器:
{{ phone | phoneCrypt('!!!!') }}
【相关推荐:javascript视频教程、vue.js教程】
The above is the detailed content of Vue computed properties and listeners and filters super detailed introduction. For more information, please follow other related articles on the PHP Chinese website!