Home  >  Article  >  Web Front-end  >  What events are there in vuejs

What events are there in vuejs

青灯夜游
青灯夜游Original
2021-09-27 18:18:362446browse

The events in vuejs are: focus, blur, click (click), dblclick, contextmen, mousemove, mouseover, mouseout, mouseup, keydown, keyup, select, wheel, etc.

What events are there in vuejs

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

Event Handler Event Handling

Event handling in Vuejs is very powerful and very important. We must learn it well.

Event Handler The reason why Vuejs puts it in a high position is based on this consideration:

  • Put the code related to the event Written independently, it is very easy to locate various logics and easy to maintain. After

  • event handler is separated, the DOM element of the page will look very simple. Easy to understand.

  • When a page is closed, the corresponding ViewModel will also be recycled. Then the various event handler defined on the page will also be garbage collected. Will not cause memory overflow.

SupportedEvent

We have seen before I’ve been to v-on:click, so what events can be supported by v-on?

As long as it is a standard HTML defined Event, it is supported by Vuejs.

  • focus (The element gains focus)
  • blur (The element loses focus)
  • click (Click the left mouse button)
  • dblclick (Double-click the left mouse button)
  • contextmenu (Single-machine right mouse button)
  • mouseover (The pointer moves to the element with event monitoring or its child elements)
  • mouseout (The pointer moves out of the element, or to its child elements Up)
  • keydown (Keyboard action: Press any key)
  • keyup (Keyboard action: Release any key)

All HTML standard events: https://developer.mozilla.org/zh-CN/docs/Web/Events

Example:

What events are there in vuejs

What events are there in vuejs

What events are there in vuejs

A total of 162 standard events are defined, and dozens of non-standard events, as well as Mozilla-specific events. As shown in the figure below:

What events are there in vuejs
We don’t need to remember them all. Usually in daily development, less than 20 are the most common events.

Usev-on Binding events

We can think that almost all events are driven by v-on this directive . Therefore, this section will provide a more detailed explanation of v-on.

1. Use variables in v-on

As shown in the following code, you can reference variables in v-on:

<html>
<head>
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
	<div id=&#39;app&#39;>
		您点击了: {% raw %}{{{% endraw %} count }} 次
		<br/>
		<button v-on:click=&#39;count += 1&#39; style=&#39;margin-top: 50px&#39;> + 1</button>
	</div>

	<script>
		var app = new Vue({
			el: &#39;#app&#39;, 
			data: {
				count: 0
			}
		})
	</script>
</body>
</html>

After opening the above code with a browser and clicking the button, you can see that the variable count will be 1. As shown in the figure below:

What events are there in vuejs

2. Use the method name in

v-on

. The above example can also be implemented as follows:

<html>
<head>
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
	<div id=&#39;app&#39;>
		您点击了:{% raw %}{{{% endraw %} count }} 次
		<br/>
		<button v-on:click=&#39;increase_count&#39; style=&#39;margin-top: 50px&#39;>  + 1 </button>
	</div>

	<script>
		var app = new Vue({
			el: &#39;#app&#39;, 
			data: {
				count: 0
			}, 
			methods: {
				increase_count: function(){
					this.count += 1
				}
			}
		})
	</script>
</body>
</html>

You can see that in v-on:click='increase_count', increase_count is a method name.

3. Use the method name parameter in v-on

We can also use v-on:click=' directly some_function("your_parameter")' is written like this, as shown in the following example:

<html>
<head>
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
	<div id=&#39;app&#39;>
		{% raw %}{{{% endraw %} message }}
		<br/>
		<button v-on:click=&#39;say_hi("明日的Vuejs大神")&#39; style=&#39;margin-top: 50px&#39;> 跟我打个招呼~ </button>
	</div>

	<script>
		var app = new Vue({
			el: &#39;#app&#39;, 
			data: {
				message: "这是个 在click中调用 方法 + 参数的例子"
			},
			methods: {
				say_hi: function(name){
					this.message = "你好啊," + name + "!"
				}
			}
		})
	</script>
</body>
</html>

After opening it with a browser, click the button, and you will see the following picture:
What events are there in vuejs

4. Redesign the logic of the button

In actual development, we often encounter situations like this: Click a button, or trigger a certain After the event, you want the default state of the button.

The most typical example: When submitting a form (<form></form>), we want to verify the form first. If verification fails, the form is not submitted.

At this time, if we want the form not to be submitted, we have to make this submit button without any next action. In all development languages, there will be a corresponding method called: "preventDefault"
(Stop the default action)

Let's look at this example:

<html>
<head>
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
	<div id=&#39;app&#39;>

		请输入您想打开的网址,   	<br/>
		判断规则是: 				<br/>
		1. 务必以 "http://"开头 	<br/>
		2. 不能是空字符串			<br/>
		<input v-model="url" placeholder="请输入 http:// 开头的字符串, 否则不会跳转" /> <br/>
		<br/>
		<a v-bind:href="this.url" v-on:click=&#39;validate($event)&#39;> 点我确定 </a>
	</div>

	<script>
		var app = new Vue({
			el: &#39;#app&#39;, 
			data: {
				url: &#39;&#39;
			}, 
			methods: {
				validate: function(event){
					if(this.url.length == 0 || this.url.indexOf(&#39;http://&#39;) != 0){
						alert("您输入的网址不符合规则。 无法跳转")
						if(event){
							alert("event is: " + event)
							event.preventDefault()
						}   
					}
				}
			}
		})
	</script>
</body>
</html>

上面的代码中,可以看到,我们定义了一个变量: url. 并且通过代码:

<a v-bind:href="this.url" v-on:click="'validate($event)'"> 点我确定 </a> 做了两件事情:

  • url 绑定到了该元素上。

  • 该元素 在触发 click事件时,会调用 validate方法。 该方法传递了一个特殊的参数: $event. 该参数是当前 事件的一个实例。(MouseEvent)

validate方法中,我们是这样定义的: 先验证是否符合规则。 如果符合,放行,会继续触发 <a></a> 元素的默认动作(让浏览器发生跳转) 。 否则的
话,会弹出一个 “alert” 提示框。

用浏览器打开这段代码,可以看到下图所示:
What events are there in vuejs
我们先输入一个合法的地址: http://baidu.com , 可以看到,点击后,页面发生了跳转。 跳转到了百度。

我们再输入一个 “不合法”的地址: https://baidu.com 注意: 该地址不是以 “http://” 开头,所以我们的vuejs 代码不会让它放行。

如下图所示:
What events are there in vuejs
进一步观察,页面也不会跳转(很好的解释了 这个时候 <a></a> 标签点了也不起作用)

5. Event Modifiers 事件修饰语

我们很多时候,希望把代码写的优雅一些。 使用传统的方式,可能会把代码写的很臃肿。 如果某个元素在不同的event下有不同的表现,那么代码看起来就会有
很多个 if ...else ... 这样的分支。

所以, Vuejs 提供了 “Event Modifiers”。

例如,我们可以把上面的例子略加修改:

<html>
<head>
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
	<div id=&#39;app&#39;>

		请输入您想打开的网址,   	<br/>
		判断规则是: 				<br/>
		1. 务必以 "http://"开头 	<br/>
		2. 不能是空字符串			<br/>
		<input v-model="url" placeholder="请输入 http:// 开头的字符串, 否则不会跳转" /> <br/>
		<br/>
		<a v-bind:href="this.url" v-on:click=&#39;validate($event)&#39; v-on:click.prevent=&#39;show_message&#39;> 点我确定 </a>
	</div>

	<script>
		var app = new Vue({
			el: &#39;#app&#39;, 
			data: {
				url: &#39;&#39;
			}, 
			methods: {
				validate: function(event){
					if(this.url.length == 0 || this.url.indexOf(&#39;http://&#39;) != 0){
						if(event){
							event.preventDefault()
						}   
					}
				},
				show_message: function(){
					alert("您输入的网址不符合规则。 无法跳转")
				}
			}
		})
	</script>
</body>
</html>

可以看出,上面的代码的核心是:

<a v-bind:href="this.url" v-on:click=&#39;validate($event)&#39; v-on:click.prevent=&#39;show_message&#39;> 点我确定 </a>

methods: {
	validate: function(event){
		if(this.url.length == 0 || this.url.indexOf(&#39;http://&#39;) != 0){
			if(event){
				event.preventDefault()
			}   
		}
	},
	show_message: function(){
		alert("您输入的网址不符合规则。 无法跳转")
	}
}

先是在 <a></a> 中定义了两个 click 事件,一个是 click, 一个是 click.prevent. 后者表示,如果该元素的click 事件被 阻止了的话, 应该触发什么动作。

然后,在 methods 代码段中,专门定义了 show_message , 用来给 click.prevent 所使用。

上面的代码运行起来,跟前一个例子是一模一样的。 只是抽象分类的程度更高了一些。 在复杂的项目中有用处。

这样的 “event modifier”,有这些:

  • stop propagation 被停止后( 也就是调用了 event.stopPropagation()方法后 ),被触发
  • prevent 调用了 event.preventDefault() 后被触发。
  • capture 子元素中的事件可以在该元素中 被触发。
  • self 事件的 event.target 就是本元素时,被触发。
  • once 该事件最多被触发一次。
  • passive 为移动设备使用。 (在addEventListeners 定义时,增加passive选项。)

以上的 “event modifier” 也可以连接起来使用。 例如: v-on:click.prevent.self

6. Key Modifiers 按键修饰语

Vuejs 也很贴心的提供了 Key Modifiers, 也就是一种支持键盘事件的快捷方法。 我们看下面的例子:

<html>
<head>
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
	<div id=&#39;app&#39;>
		输入完毕后,按下回车键,就会<br/>
		触发 "show_message" 事件~  <br/><br/>

		<input v-on:keyup.enter="show_message" v-model="message" />
	</div>

	<script>
		var app = new Vue({
			el: &#39;#app&#39;, 
			data: {
				message: &#39;&#39;
			}, 
			methods: {
				show_message: function(){
					alert("您输入了:" + this.message)
				}
			}
		})
	</script>
</body>
</html>

可以看到,在上面的代码中, v-on:keyup.enter="show_message"<a></a> 元素定义了事件,该事件对应了 “回车键”。
(严格的说,是回车键被按下后,松开弹起来的那一刻)

我们用浏览器打开上面的代码对应的文件,输入一段文字,按回车,就可以看到事件已经被触发了。 

Vuejs 总共支持下面这些 Key modifiers:

  • enter 回车键
  • tab tab 键
  • delete 同时对应了 backspace 和 del 键
  • esc ESC 键
  • space 空格
  • up 向上键
  • down 向下键
  • left 向左键
  • right 向右键

随着 Vuejs 版本的不断迭代和更新,越来越多的 Key modifiers 被添加了进来, 例如 page down, ctrl 。对于这些键的用法,
大家可以查阅官方文档。

相关推荐:《vue.js教程

The above is the detailed content of What events are there in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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