Detailed explanation of VUE's data proxy and events

Detailed explanation of VUE's data proxy and events

Review of Object.defineProperty method

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Review of Object.defineproperty method</title>
	</head>
	<body>
		<script type="text/javascript" >
			let number = 18
			let person = {
				name:'Zhang San',
				sex:'male',
			}
 			Object.defineProperty(person,'age',{
				// value:18,
				// enumerable:true, //controls whether the property can be enumerated, the default value is false
				// writable:true, //controls whether the property can be modified, the default value is false
				// configurable: true //Controls whether the property can be deleted. The default value is false
 				//When someone reads the age property of person, the get function (getter) will be called and the return value is the value of age get(){
					console.log('Someone read the age property')
					return number
				},
 				//When someone modifies the age property of person, the setter function will be called and receive the modified value set(value){
					console.log('Someone modified the age property, and the value is', value)
					number = value
				}
 			})
 			// console.log(Object.keys(person))
 			console.log(person)
		</script>
	</body>
</html>

What is a data broker?

Data proxy: Operation (read/write) of attributes in another object through an object proxy

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>What is a data agent?</title>
	</head>
	<body>
		<!-- Data proxy: operation (read/write) of attributes in another object through an object proxy -->
		<script type="text/javascript" >
			let obj = {x:100}
			let obj2 = {y:200}
 			Object.defineProperty(obj2,'x',{
				get(){
					return obj.x
				},
				set(value){
					obj.x = value
				}
			})
		</script>
	</body>
</html>

Data Proxy in Vue

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Data Proxy in Vue</title>
		<!-- Import Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
				1. Data proxy in Vue:
							Use the vm object to proxy the operation (read/write) of the attributes in the data object
				2. Benefits of data proxy in Vue:
							More convenient operation of data in data 3. Basic principle:
							Add all properties in the data object to vm through Object.defineProperty().
							For each property added to the vm, specify a getter/setter.
							Operate (read/write) the corresponding attributes in data inside the getter/setter.
		 -->
		<!-- Prepare a container -->
		<div id="root">
			<h2>School name: {{name}}</h2>
			<h2>School address: {{address}}</h2>
		</div>
	</body>
 	<script type="text/javascript">
		Vue.config.productionTip = false //Prevent Vue from generating production tips at startup.
 		const vm = new Vue({
			el:'#root',
			data:{
				name:'Shang Silicon Valley',
				address:'Hongfu Technology Park'
			}
		})
	</script>
</html>

image

Basic use of events

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Data Proxy in Vue</title>
		<!-- Import Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
				1. Data proxy in Vue:
							Use the vm object to proxy the operation (read/write) of the attributes in the data object
				2. Benefits of data proxy in Vue:
							More convenient operation of data in data 3. Basic principle:
							Add all properties in the data object to vm through Object.defineProperty().
							For each property added to the vm, specify a getter/setter.
							Operate (read/write) the corresponding attributes in data inside the getter/setter.
		 -->
		<!-- Prepare a container -->
		<div id="root">
			<h2>School name: {{name}}</h2>
			<h2>School address: {{address}}</h2>
		</div>
	</body>
 	<script type="text/javascript">
		Vue.config.productionTip = false //Prevent Vue from generating production tips at startup.
 		const vm = new Vue({
			el:'#root',
			data:{
				name:'Shang Silicon Valley',
				address:'Hongfu Technology Park'
			}
		})
	</script>
</html>

Event modifiers

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Basic use of events</title>
		<!-- Import Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
				Basic use of events:
							1. Use v-on:xxx or @xxx to bind the event, where xxx is the event name;
							2. The event callback needs to be configured in the methods object and will eventually be on the vm;
							3. Do not use arrow functions for functions configured in methods! Otherwise this is not vm;
							4. The functions configured in methods are all functions managed by Vue, and this points to vm or component instance object;
							5. @click="demo" and @click="demo($event)" have the same effect, but the latter can pass parameters;
		-->
		<!-- Prepare a container -->
		<div id="root">
			<h2>Welcome to {{name}} study</h2>
			<!-- <button v-on:click="showInfo">Click me for info</button> -->
			<button @click="showInfo1">Click me for information 1 (no parameters)</button>
			<button @click="showInfo2($event,66)">Click me for prompt information 2 (parameter passing)</button>
		</div>
	</body>
 	<script type="text/javascript">
		Vue.config.productionTip = false //Prevent Vue from generating production tips at startup.
 		const vm = new Vue({
			el:'#root',
			data:{
				name:'Shang Silicon Valley',
			},
			methods:{
				showInfo1(event){
					// console.log(event.target.innerText)
					// console.log(this) //This is vm
					alert('Hello, classmate!')
				},
				showInfo2(event,number){
					console.log(event,number)
					// console.log(event.target.innerText)
					// console.log(this) //This is vm
					alert('Hello classmate!!')
				}
			}
		})
	</script>
</html>

Keyboard events

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Keyboard Events</title>
		<!-- Import Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
				1. Commonly used button aliases in Vue:
							Enter => enter
							Delete => delete (captures "delete" and "backspace" keys)
							Exit => esc
							Space => space
							Line break => tab (special, must be used with keydown)
							Up => up
							down => down
							left => left
							Right => right
 				2. For buttons that Vue does not provide aliases, you can use the original key value of the button to bind, but be careful to convert it to kebab-case (short dash naming)
 				3. System modifier keys (special usage): ctrl, alt, shift, meta
							(1) Used with keyup: Press the modifier key, then press other keys, and then release the other keys, the event will be triggered.
							(2) Used with keydown: trigger events normally.
 				4. You can also use keyCode to specify specific keys (not recommended)
 				5.Vue.config.keyCodes.Custom key name = key code, you can customize the key alias -->
		<!-- Prepare a container -->
		<div id="root">
			<h2>Welcome to {{name}} study</h2>
			<input type="text" placeholder="Press Enter to prompt input" @keydown.huiche="showInfo">
		</div>
	</body>
 	<script type="text/javascript">
		Vue.config.productionTip = false //Prevent Vue from generating production tips at startup.
		Vue.config.keyCodes.huiche = 13 // defines an alias key new Vue({
			el:'#root',
			data:{
				name:'Shang Silicon Valley'
			},
			methods: {
				showInfo(e){
					// console.log(e.key,e.keyCode)
					console.log(e.target.value)
				}
			},
		})
	</script>
</html>

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • A brief discussion on the principle of Vue's two-way event binding v-model
  • Detailed explanation of Vue dynamic components and global event binding summary
  • Usage of Vue filters and timestamp conversion issues
  • Detailed explanation of Vue filter implementation and application scenarios
  • Detailed explanation of Vue's data and event binding and filter filters

<<:  How to restore data using binlog in mysql5.7

>>:  How to redraw Button as a circle in XAML

Recommend

Basic knowledge: What does http mean before a website address?

What is HTTP? When we want to browse a website, w...

CSS implements the web component function of sliding the message panel

Hello everyone, I wonder if you have the same con...

Solve the problem of MySql8.0 checking transaction isolation level error

Table of contents MySql8.0 View transaction isola...

MySQL Tutorial: Subquery Example Detailed Explanation

Table of contents 1. What is a subquery? 2. Where...

mysql method to recursively search for all child nodes of a menu node

background There is a requirement in the project ...

Briefly describe the use and description of MySQL primary key and foreign key

Table of contents 1. Foreign key constraints What...

How to set up Windows Server 2019 (with pictures and text)

1. Windows Server 2019 Installation Install Windo...

The marquee tag in HTML achieves seamless scrolling marquee effect

The <marquee> tag is a tag that appears in ...

MySQL 5.7.19 installation and configuration method graphic tutorial (win10)

Detailed tutorial on downloading and installing M...

Reasons and solutions for the failure of React event throttling effect

Table of contents The problem here is: Solution 1...

4 solutions to CSS browser compatibility issues

Front-end is a tough job, not only because techno...

Use of Docker UI, a Docker visualization management tool

1. Introduction to DockerUI DockerUI is based on ...

Vue application example code based on axios request encapsulation

Table of contents What is axios? Axios request ty...