vue使用once修飾符,使事件只能觸發(fā)一次問題
更新時間:2022年05月30日 09:17:15 作者:weixin_41201496
這篇文章主要介紹了vue使用once修飾符,使事件只能觸發(fā)一次問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
once修飾符,使事件只能觸發(fā)一次
多個修飾符可以同步使用
<!--定義vue的操作對象--> <div id="app"> ? ? <!-- 使用self修飾符 點擊標簽自身時才會執(zhí)行事件 --> ? ? <!-- 使用once修飾符 使事件只能觸發(fā)一次 ?--> ? ? <!-- 多個修飾符可以同時使用 ? --> ? ? <div class="inner" @click.self.once="divClick"> ? ? ? ? <input type="button" value="點擊" @click="butClick"> ? ? </div> </div>
<!--導(dǎo)入vue.js-->
<script src="./vue.js"></script>
<script>
? ? //創(chuàng)建一個vue實例
? ? var vm = new Vue({
? ? ? ? el:"#app", //指定實例控制的DOM元素
? ? ? ? data:{ //存儲頁面數(shù)據(jù)
? ? ? ? },
? ? ? ? methods:{ //在此處定義實例可用的所有方法
? ? ? ? ? ? divClick(){
? ? ? ? ? ? ? ? console.log('div點擊事件')
? ? ? ? ? ? },
? ? ? ? ? ? butClick(){
? ? ? ? ? ? ? ? console.log('button點擊事件')
? ? ? ? ? ? }
? ? ? ? }
? ? })
</script>vue事件修飾符(once:prev:stop)
附有同一文件夾下的html文件、js文件和css文件
注釋說的很詳細
index.html的代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue</title>
<link rel="stylesheet" href="style.css" rel="external nofollow" >
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<!--vue-app是根容器-->
<div id="vue-app">
<h1>Event</h1>
<button @click.once="add(1)">單擊漲一歲</button>
<button v-on:click="subtract(1)">單擊減一歲</button>
<button @dblclick="add(10)">雙擊漲十歲</button>
<button v-on:dblclick="subtract(10)">雙擊減十歲</button>
<p>My age is {{age}}</p>
<div id="canvas" v-on:mousemove="updateXY">
{{x}},{{y}} -
<span v-on:mousemove="stopMoving">Stop Moving</span>
<br>
<span v-on:mousemove.stop="">Stop Moving</span>
</div>
<a v-on:click="alert()" rel="external nofollow" rel="external nofollow" >baidu</a>
<br>
<a v-on:click.prevent="alert()" rel="external nofollow" rel="external nofollow" >baidu</a>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
app.js的代碼
//實例化VUE對象
new Vue({
el:"#vue-app",
//僅限于在vue-app容器下
data:{
age:30,
x:0,
y:0
},
methods:{
add:function(inc){
this.age += inc;
},
subtract:function(dec){
this.age -= dec;
},
updateXY:function(event){
this.x = event.offsetX;
this.y = event.offsetY;
},
stopMoving:function(event){
event.stopPropagation();
},
alert:function(){
alert("Hellow world !");
}
}
});
style.css代碼
#canvas{
width: 600px;
padding: 200px 20px;
text-align: center;
border: 1px solid #333;
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue分別運用class綁定和style綁定通過點擊實現(xiàn)樣式切換
這篇文章主要為大家介紹了Vue分別運用class綁定和style綁定通過點擊實現(xiàn)樣式切換,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Vue聲明式導(dǎo)航與編程式導(dǎo)航示例分析講解
這篇文章主要介紹了Vue中聲明式導(dǎo)航與編程式導(dǎo)航,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11

