Vue?click事件傳遞參數(shù)的示例教程
簡介
說明
本文用示例介紹Vue中事件傳參的方法。
Vue的事件用法為:v-on:click="xxx"??梢杂聾click="xxx"來簡寫。
本處采用click這個事件進行展示,其他的事件也是一樣的。
官網(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')">點我</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ù)時,默認會傳遞事件。當然也可以通過$event來傳遞事件。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this is title</title>
</head>
<body>
<div id="app">
<button @click="clickHere">點我</button>
<!--等價于下邊這個-->
<!--<button @click="clickHere($event)">點我</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')">點我</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é)果

動態(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)">點我</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é)果

動態(tài)參數(shù)(從全局數(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})">點我</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)址
到此這篇關(guān)于Vue click事件傳遞參數(shù)--方法/教程/實例的文章就介紹到這了,更多相關(guān)Vue click事件傳遞參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?demi支持sfc方式的vue2vue3通用庫開發(fā)詳解
這篇文章主要為大家介紹了vue?demi支持sfc方式的vue2vue3通用庫開發(fā)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
mpvue微信小程序開發(fā)之實現(xiàn)一個彈幕評論
這篇文章主要介紹了mpvue小程序開發(fā)之 實現(xiàn)一個彈幕評論功能,本文通過實例講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
vue-router之nuxt動態(tài)路由設(shè)置的兩種方法小結(jié)
今天小編就為大家分享一篇vue-router之nuxt動態(tài)路由設(shè)置的兩種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實現(xiàn)
本文主要介紹了vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

