Vue?click事件傳遞參數(shù)的示例教程
簡介
說明
本文用示例介紹Vue中事件傳參的方法。
Vue的事件用法為:v-on:click="xxx"??梢杂聾click="xxx"來簡寫。
本處采用click這個事件進(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ù)時,默認(rèn)會傳遞事件。當(dāng)然也可以通過$event來傳遞事件。
<!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> <!--等價于下邊這個--> <!--<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é)果
動態(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é)果
動態(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)址
到此這篇關(guān)于Vue click事件傳遞參數(shù)--方法/教程/實例的文章就介紹到這了,更多相關(guān)Vue click事件傳遞參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用this.$router.go(-1)遇到的一些問題及解決
這篇文章主要介紹了使用this.$router.go(-1)遇到的一些問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Vue + OpenLayers 快速入門學(xué)習(xí)教程
大家都知道使用 Openlayers可以很靈活自由的做出各種地圖和空間數(shù)據(jù)的展示。而且這個框架是完全免費(fèi)和開源的,本文記錄下 Vue 使用 OpenLayers 入門,使用 OpenLayers 創(chuàng)建地圖組件的相關(guān)知識,需要的朋友一起學(xué)習(xí)下吧2021-09-09vite+vue3項目解決低版本兼容性問題解決方案(Safari白屏)
這篇文章主要介紹了vite+vue3項目解決低版本兼容性問題(Safari白屏),使用官方插件 @vitejs/plugin-legacy 為打包后的文件提供傳統(tǒng)瀏覽器兼容性支持,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03