1、監(jiān)聽事件
我們可以使用指令v-on 或簡寫方式@來監(jiān)聽js事件觸發(fā)肴焊。
<div id="basic-event">
<button @click="counter += 1">Add 1</button>
<p>The button above has been clicked {{ counter }} times.</p>
</div>
Vue.createApp({
data() {
return {
counter: 1
}
}
}).mount('#basic-event')
2馒过、事件監(jiān)聽方法
僅用方法名稱可以返回event
<div id="event-with-method">
<!-- `greet` is the name of a method defined below -->
<button @click="greet">Greet</button>
</div>
Vue.createApp({
data() {
return {
name: 'Vue.js'
}
},
methods: {
greet(event) {
// `this` inside methods points to the current active instance
alert('Hello ' + this.name + '!')
// `event` is the native DOM event
if (event) {
alert(event.target.tagName)
}
}
}
}).mount('#event-with-method')
方法傳參數(shù)
<div id="inline-handler">
<button @click="say('hi')">Say hi</button>
<button @click="say('what')">Say what</button>
</div>
Vue.createApp({
methods: {
say(message) {
alert(message)
}
}
}).mount('#inline-handler')
如果想傳參同時需要處理event ,我們可以用$event
<button @click="warn('Form cannot be submitted yet.', $event)">
Submit
</button>
methods: {
warn(message, event) {
// now we have access to the native event
if (event) {
event.preventDefault()
}
alert(message)
}
}
綁定多個方法
<!-- both one() and two() will execute on button click -->
<button @click="one($event), two($event)">
Submit
</button>
// ...
methods: {
one(event) {
// first handler logic...
},
two(event) {
// second handler logic...
}
}
3笨鸡、事件修飾器
event.preventDefault()
event.stopPropagation()
.stop
.prevent
.capture
.self
.once
.passive
<!-- the click event's propagation will be stopped -->
<a @click.stop="doThis"></a>
<!-- the submit event will no longer reload the page -->
<form @submit.prevent="onSubmit"></form>
<!-- modifiers can be chained -->
<a @click.stop.prevent="doThat"></a>
<!-- just the modifier -->
<form @submit.prevent></form>
<!-- use capture mode when adding the event listener -->
<!-- i.e. an event targeting an inner element is handled here before being handled by that element -->
<div @click.capture="doThis">...</div>
<!-- only trigger handler if event.target is the element itself -->
<!-- i.e. not from a child element -->
<div @click.self="doThat">...</div>
注:使用修飾符時,順序很重要坦冠,因為相關(guān)代碼是按照相同的順序生成的形耗。因此使用@click.prevent。當@click.self時辙浑,self將阻止所有點擊激涤。prevent只會阻止對元素本身的單擊。
<!-- the click event will be triggered at most once -->
<a @click.once="doThis"></a>
.passive 相當于addEventListener
<!-- the scroll event's default behavior (scrolling) will happen -->
<!-- immediately, instead of waiting for `onScroll` to complete -->
<!-- in case it contains `event.preventDefault()` -->
<div @scroll.passive="onScroll">...</div>
注:不要同時使用.passive和.prevent判呕,因為.prevent會被忽略倦踢,你的瀏覽器可能會向你顯示一個警告。請記住.passives是與瀏覽器通信侠草,表示您不想阻止該事件的默認行為辱挥。
4、鍵盤事件
<!-- only call `vm.submit()` when the `key` is `Enter` -->
<input @keyup.enter="submit" />
<input @keyup.page-down="onPageDown" />
.enter
.tab
.delete (captures both "Delete" and "Backspace" keys)
.esc
.space
.up
.down
.left
.right
.ctrl
.alt
.shift
.meta
<!-- Alt + Enter -->
<input @keyup.alt.enter="clear" />
<!-- Ctrl + Click -->
<div @click.ctrl="doSomething">Do something</div>
.exact 修飾符允許你控制由精確的系統(tǒng)修飾符組合觸發(fā)的事件
<!-- this will fire even if Alt or Shift is also pressed -->
<button @click.ctrl="onClick">A</button>
<!-- this will only fire when Ctrl and no other keys are pressed -->
<button @click.ctrl.exact="onCtrlClick">A</button>
<!-- this will only fire when no system modifiers are pressed -->
<button @click.exact="onClick">A</button>
鼠標修飾符
.left
.right
.middle