Vue 實(shí)例事件簡(jiǎn)單示例
本文實(shí)例講述了Vue 實(shí)例事件。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue 實(shí)例事件</title>
<script type="text/javascript" src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
</head>
<body>
<h1>Vue 實(shí)例事件</h1>
<hr>
<div id="app">
<p>
{{num}}
</p>
<p><button @click="add">add</button></p>
</div>
<p><button οnclick="reduce()">reduce</button></p>
<p><button οnclick="reduceonce()">reduceonce</button></p>
<p><button οnclick="off()">關(guān)閉事件</button></p>
</body>
</html>
<script>
var app = new Vue({
el:'#app',
data:{
num:1
},
methods:{
add:function(){
this.num++
}
},
})
//在構(gòu)造器 on 一直調(diào)用 once只能調(diào)用一次
app.$on('reduce',function(){
this.num--;
console.log('執(zhí)行了reduce方法')
})
app.$once('reduceonce',function(){
this.num--;
console.log('執(zhí)行了reduceonce方法')
})
function reduce(){
//emit 觸發(fā)當(dāng)前實(shí)例上的事件
app.$emit('reduce');
}
function reduceonce(){
app.$emit('reduceonce');
}
// $off 關(guān)閉事件
function off(){
app.$off('reduce');
console.log('關(guān)閉了reduce')
}
</script>
運(yùn)行結(jié)果:

感興趣的朋友可以使用在線(xiàn)HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
- vue父組件點(diǎn)擊觸發(fā)子組件事件的實(shí)例講解
- 對(duì)vue 鍵盤(pán)回車(chē)事件的實(shí)例講解
- vue2.0移動(dòng)端滑動(dòng)事件vue-touch的實(shí)例代碼
- Vue的事件響應(yīng)式進(jìn)度條組件實(shí)例詳解
- vue鼠標(biāo)懸停事件實(shí)例詳解
- vue實(shí)現(xiàn)鼠標(biāo)移入移出事件代碼實(shí)例
- vue綁定的點(diǎn)擊事件阻止冒泡的實(shí)例
- vue父組件觸發(fā)事件改變子組件的值的方法實(shí)例詳解
- 對(duì)vue中v-on綁定自定事件的實(shí)例講解
- Vue動(dòng)畫(huà)事件詳解及過(guò)渡動(dòng)畫(huà)實(shí)例
- vue基礎(chǔ)之事件簡(jiǎn)寫(xiě)、事件對(duì)象、冒泡、默認(rèn)行為、鍵盤(pán)事件實(shí)例分析
- Vue 事件處理操作實(shí)例詳解
相關(guān)文章
Vue.js實(shí)現(xiàn)模擬微信朋友圈開(kāi)發(fā)demo
本篇文章主要介紹了Vue.js實(shí)現(xiàn)模擬微信朋友圈開(kāi)發(fā)demo,實(shí)現(xiàn)展示朋友圈,評(píng)論,點(diǎn)贊等功能,有興趣的可以了解一下。2017-04-04
實(shí)現(xiàn)elementUI表單的全局驗(yàn)證的方法步驟
這篇文章主要介紹了實(shí)現(xiàn)elementUI表單的全局驗(yàn)證的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue(element ui)使用websocket及心跳檢測(cè)方式
這篇文章主要介紹了vue(element ui)使用websocket及心跳檢測(cè)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue中v-for循環(huán)選中點(diǎn)擊的元素并對(duì)該元素添加樣式操作
這篇文章主要介紹了vue中v-for循環(huán)選中點(diǎn)擊的元素并對(duì)該元素添加樣式操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

