Vue?click事件傳遞參數(shù)的示例教程
簡(jiǎn)介
說(shuō)明
本文用示例介紹Vue中事件傳參的方法。
Vue的事件用法為:v-on:click="xxx"??梢杂聾click="xxx"來(lái)簡(jiǎn)寫。
本處采用click這個(gè)事件進(jìn)行展示,其他的事件也是一樣的。
官網(wǎng)
只傳自定義參數(shù)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this is title</title>
</head>
<body>
<div id="app">
<button @click="clickHere('hello')">點(diǎn)我</button>
</div>
<script src="js/vue.js"></script>
<script>Vue.config.productionTip = false</script>
<script>
let vm = new Vue({
el: '#app',
methods: {
clickHere: function (param1) {
console.log("參數(shù):");
console.log(param1);
}
}
})
</script>
</body>
</html>結(jié)果

只傳事件參數(shù)
不指定參數(shù)時(shí),默認(rèn)會(huì)傳遞事件。當(dāng)然也可以通過(guò)$event來(lái)傳遞事件。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this is title</title>
</head>
<body>
<div id="app">
<button @click="clickHere">點(diǎn)我</button>
<!--等價(jià)于下邊這個(gè)-->
<!--<button @click="clickHere($event)">點(diǎn)我</button>-->
</div>
<script src="js/vue.js"></script>
<script>Vue.config.productionTip = false</script>
<script>
let vm = new Vue({
el: '#app',
methods: {
clickHere: function (e) {
console.log("事件:");
console.log(e);
}
}
})
</script>
</body>
</html>
結(jié)果

傳事件和自定義參數(shù)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this is title</title>
</head>
<body>
<div id="app">
<button @click="clickHere($event, 'hello')">點(diǎn)我</button>
</div>
<script src="js/vue.js"></script>
<script>Vue.config.productionTip = false</script>
<script>
let vm = new Vue({
el: '#app',
methods: {
clickHere: function (event, param1) {
console.log("事件:");
console.log(event);
console.log("參數(shù):");
console.log(param1);
}
}
})
</script>
</body>
</html>
結(jié)果

動(dòng)態(tài)參數(shù)(從局部取值)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this is title</title>
</head>
<body>
<div id="app">
<div v-for="hero in heros">
<button @click="clickHere(hero.name)">點(diǎn)我</button>
</div>
</div>
<script src="js/vue.js"></script>
<script>Vue.config.productionTip = false</script>
<script>
let vm = new Vue({
el: '#app',
methods: {
clickHere: function (param1) {
console.log("參數(shù):");
console.log(param1);
}
},
data: {
heros: [{
name: "Iron Man",
age: 30
}, {
name: "Captain America",
age: 40
}]
}
})
</script>
</body>
</html>
結(jié)果

動(dòng)態(tài)參數(shù)(從全局?jǐn)?shù)據(jù)取值)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this is title</title>
</head>
<body>
<div id="app">
<button @click="clickHere({message})">點(diǎn)我</button>
</div>
<script src="js/vue.js"></script>
<script>Vue.config.productionTip = false</script>
<script>
let vm = new Vue({
el: '#app',
methods: {
clickHere: function (param1) {
console.log("參數(shù):");
console.log(param1);
}
},
data: {
message: "hello world"
}
})
</script>
</body>
</html>
結(jié)果

其他網(wǎng)址
vue click同時(shí)傳入事件對(duì)象和自定義參數(shù)
到此這篇關(guān)于Vue click事件傳遞參數(shù)--方法/教程/實(shí)例的文章就介紹到這了,更多相關(guān)Vue click事件傳遞參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)內(nèi)容可滾動(dòng)的彈窗效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)內(nèi)容可滾動(dòng)的彈窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue?demi支持sfc方式的vue2vue3通用庫(kù)開發(fā)詳解
這篇文章主要為大家介紹了vue?demi支持sfc方式的vue2vue3通用庫(kù)開發(fā)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
3分鐘帶你快速認(rèn)識(shí)Vue3中的v-model
model在vue里面實(shí)現(xiàn)雙向綁定,通過(guò)父節(jié)點(diǎn)向子節(jié)點(diǎn)傳遞參數(shù),子節(jié)點(diǎn)通過(guò)操作再回傳給父節(jié)點(diǎn)的變量,有點(diǎn)像prop和$emit組合使用,下面這篇文章主要給大家介紹了關(guān)于Vue3中v-model的相關(guān)資料,需要的朋友可以參考下2022-11-11
mpvue微信小程序開發(fā)之實(shí)現(xiàn)一個(gè)彈幕評(píng)論
這篇文章主要介紹了mpvue小程序開發(fā)之 實(shí)現(xiàn)一個(gè)彈幕評(píng)論功能,本文通過(guò)實(shí)例講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
vue-router之nuxt動(dòng)態(tài)路由設(shè)置的兩種方法小結(jié)
今天小編就為大家分享一篇vue-router之nuxt動(dòng)態(tài)路由設(shè)置的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue3解決跨域問(wèn)題詳細(xì)代碼親測(cè)有效
跨域,跨的是不同域,也就是協(xié)議或主機(jī)或或端口號(hào)不同造成的現(xiàn)象,本文給大家分享vue3解決跨域問(wèn)題詳細(xì)代碼親測(cè)有效,感興趣的朋友跟隨小編一起看看吧2022-11-11
vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實(shí)現(xiàn)
本文主要介紹了vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

