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)文章
使用this.$router.go(-1)遇到的一些問(wèn)題及解決
這篇文章主要介紹了使用this.$router.go(-1)遇到的一些問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Vue + OpenLayers 快速入門學(xué)習(xí)教程
大家都知道使用 Openlayers可以很靈活自由的做出各種地圖和空間數(shù)據(jù)的展示。而且這個(gè)框架是完全免費(fèi)和開(kāi)源的,本文記錄下 Vue 使用 OpenLayers 入門,使用 OpenLayers 創(chuàng)建地圖組件的相關(guān)知識(shí),需要的朋友一起學(xué)習(xí)下吧2021-09-09preload對(duì)比prefetch的功能區(qū)別詳解
這篇文章主要為大家介紹了preload對(duì)比prefetch的使用區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06基于vue2的table分頁(yè)組件實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了基于vue2的table分頁(yè)組件實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03vite+vue3項(xiàng)目解決低版本兼容性問(wèn)題解決方案(Safari白屏)
這篇文章主要介紹了vite+vue3項(xiàng)目解決低版本兼容性問(wèn)題(Safari白屏),使用官方插件 @vitejs/plugin-legacy 為打包后的文件提供傳統(tǒng)瀏覽器兼容性支持,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03