The events in vuejs are: focus, blur, click (click), dblclick, contextmen, mousemove, mouseover, mouseout, mouseup, keydown, keyup, select, wheel, etc.
The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.
Event Handler
Event HandlingEvent 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:
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:
We don’t need to remember them all. Usually in daily development, less than 20 are the most common events.
v-on
Binding eventsWe 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
.
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='app'> 您点击了: {% raw %}{{{% endraw %} count }} 次 <br/> <button v-on:click='count += 1' style='margin-top: 50px'> + 1</button> </div> <script> var app = new Vue({ el: '#app', 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:
in . 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='app'> 您点击了:{% raw %}{{{% endraw %} count }} 次 <br/> <button v-on:click='increase_count' style='margin-top: 50px'> + 1 </button> </div> <script> var app = new Vue({ el: '#app', 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.
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='app'> {% raw %}{{{% endraw %} message }} <br/> <button v-on:click='say_hi("明日的Vuejs大神")' style='margin-top: 50px'> 跟我打个招呼~ </button> </div> <script> var app = new Vue({ el: '#app', 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:
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/>
), 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='app'> 请输入您想打开的网址, <br/> 判断规则是: <br/> 1. 务必以 "http://"开头 <br/> 2. 不能是空字符串 <br/> <input v-model="url" placeholder="请输入 http:// 开头的字符串, 否则不会跳转" /> <br/> <br/> <a v-bind:href="this.url" v-on:click='validate($event)'> 点我确定 </a> </div> <script> var app = new Vue({ el: '#app', data: { url: '' }, methods: { validate: function(event){ if(this.url.length == 0 || this.url.indexOf('http://') != 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/>
元素的默认动作(让浏览器发生跳转) 。 否则的
话,会弹出一个 “alert
” 提示框。
用浏览器打开这段代码,可以看到下图所示:
我们先输入一个合法的地址: http://baidu.com , 可以看到,点击后,页面发生了跳转。 跳转到了百度。
我们再输入一个 “不合法”的地址: https://baidu.com 注意: 该地址不是以 “http://” 开头,所以我们的vuejs
代码不会让它放行。
如下图所示:
进一步观察,页面也不会跳转(很好的解释了 这个时候 <a/>
标签点了也不起作用)
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='app'> 请输入您想打开的网址, <br/> 判断规则是: <br/> 1. 务必以 "http://"开头 <br/> 2. 不能是空字符串 <br/> <input v-model="url" placeholder="请输入 http:// 开头的字符串, 否则不会跳转" /> <br/> <br/> <a v-bind:href="this.url" v-on:click='validate($event)' v-on:click.prevent='show_message'> 点我确定 </a> </div> <script> var app = new Vue({ el: '#app', data: { url: '' }, methods: { validate: function(event){ if(this.url.length == 0 || this.url.indexOf('http://') != 0){ if(event){ event.preventDefault() } } }, show_message: function(){ alert("您输入的网址不符合规则。 无法跳转") } } }) </script> </body> </html>
可以看出,上面的代码的核心是:
<a v-bind:href="this.url" v-on:click='validate($event)' v-on:click.prevent='show_message'> 点我确定 </a> methods: { validate: function(event){ if(this.url.length == 0 || this.url.indexOf('http://') != 0){ if(event){ event.preventDefault() } } }, show_message: function(){ alert("您输入的网址不符合规则。 无法跳转") } }
先是在 <a/>
中定义了两个 click 事件,一个是 click
, 一个是 click.prevent
. 后者表示,如果该元素的click 事件被 阻止了的话, 应该触发什么动作。
然后,在 methods
代码段中,专门定义了 show_message
, 用来给 click.prevent
所使用。
上面的代码运行起来,跟前一个例子是一模一样的。 只是抽象分类的程度更高了一些。 在复杂的项目中有用处。
这样的 “event modifier”,有这些:
以上的 “event modifier” 也可以连接起来使用。 例如: v-on:click.prevent.self
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='app'> 输入完毕后,按下回车键,就会<br/> 触发 "show_message" 事件~ <br/><br/> <input v-on:keyup.enter="show_message" v-model="message" /> </div> <script> var app = new Vue({ el: '#app', data: { message: '' }, methods: { show_message: function(){ alert("您输入了:" + this.message) } } }) </script> </body> </html>
可以看到,在上面的代码中, v-on:keyup.enter="show_message"
为 <a></a>
元素定义了事件,该事件对应了 “回车键”。
(严格的说,是回车键被按下后,松开弹起来的那一刻)
我们用浏览器打开上面的代码对应的文件,输入一段文字,按回车,就可以看到事件已经被触发了。
Vuejs 总共支持下面这些 Key modifiers:
随着 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!