How to use dynamic parameters and calculated properties in Vue

How to use dynamic parameters and calculated properties in Vue

1. Dynamic parameters

Starting from 2.6.0, you can use a JavaScript expression enclosed in square brackets as a directive argument:

<a v-bind:[attributeName]="url"> …

Here, attributeName will be dynamically evaluated as a JavaScript expression, and the resulting value will be used as the final parameter. For example, if your Vue instance has a data property attributeName with a value of "href", then this binding will be equivalent to v-bind:href.

Likewise, you can use dynamic parameters to bind a handler function to a dynamic event name:

<a v-on:[eventName]="doSomething"> …

In this example, when the value of eventName is "focus", v-on:[eventName] will be equivalent to v-on:focus.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Dynamic Parameters</title>
		<script src="vue.js"></script>
	</head>
	<body>
		<div id='app7'>
			<span v-on:[event_name]='dosomething'>{{msg}}</span>
		</div>
	</body>
	<script>
		var vm = new Vue({
			el:"#app7",
			data:{
				msg:100,
				event_name:'click'
			 },
			methods:{
				dosomething:function(){
					this.msg = this.msg + 1
				}
			}
		})
	</script>
</html>

2. Calculated properties

Expressions in templates are very convenient, but they are designed primarily for simple calculations. Putting too much logic in a template can make it cumbersome and difficult to maintain. For example:

{{ message.split('').reverse().join('') }}

At this point, the template is no longer just a simple declarative logic. You have to look at it for a while before you realize that what we want here is to display the reverse string of the variable message. It becomes even more difficult to handle when you want to include this flipped string in multiple places in your template.

So, for any complex logic, you should use computed properties.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Computed Properties</title>
		<script src="vue.js"></script>
	</head>
	<body>
		<div id = 'app'>{{value_add}}</div>
	</body>
	<script>
		var vm = new Vue({
			el:"#app",
			data:{
				value:100
			},
			
			computed:{ //Similar to methods value_add:function(){
					return this.value + 100
				}
			}
		})
	</script>
</html>

Summarize

This is the end of this article about the use of dynamic parameters and calculated properties in Vue. For more relevant Vue dynamic parameters and calculated properties content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of calculated properties, monitoring properties and life cycle in Vue.js
  • Introduction to Computed Properties in Vue
  • Computed properties and data acquisition methods in Vue
  • Computed properties and monitoring properties in Vue
  • Vue calculation properties and function selection

<<:  Summary of common Linux distribution mirror source configuration

>>:  MySQL 5.7.25 installation and configuration method graphic tutorial

Recommend

How to get the intersection/difference/union of two sets in mysql

Common scenarios of MySQL: getting the intersecti...

The difference between Div and table in HTML (discussed in detail in all aspects)

1: Differences in speed and loading methods The di...

Detailed explanation of the basic functions and usage of MySQL foreign keys

This article uses examples to illustrate the basi...

Detailed explanation of the difference between CSS link and @import

How to add css in html? There are three ways to s...

Analysis of the process of implementing Nginx+Tomcat cluster under Windwos

Introduction: Nginx (pronounced the same as engin...

Detailed explanation of the difference between var, let and const in JavaScript

Table of contents As a global variable Variable H...

Two ways to connect WeChat mini program to Tencent Maps

I've been writing a WeChat applet recently an...

Analyze the selection problem of storing time and date types in MySQL

In general applications, we use timestamp, dateti...

Modularity in Node.js, npm package manager explained

Table of contents The basic concept of modularity...

How to add shortcut commands in Xshell

As a useful terminal emulator, Xshell is often us...

Explore VMware ESXI CLI common commands

Table of contents 【Common commands】 [Summary of c...

HTML tag default style arrangement

html, address,blockquote,body, dd, div,dl, dt, fie...

Detailed example of jQuery's chain programming style

The implementation principle of chain programming...

JavaScript canvas to achieve raindrop effect

This article example shares the specific code for...

Vue uses echart to customize labels and colors

This article example shares the specific code of ...