vue中如何通過(guò)函數(shù)傳參數(shù)
vue通過(guò)函數(shù)傳參數(shù)
一,通過(guò)點(diǎn)擊事件本身的js特性傳參。
<view class="center_menu"> ?? ??? ??? ??? ?<view class="menu_item" v-for="item in menus" @click="toAddress(item.address)"> ?? ??? ??? ??? ??? ?<image :src="item.icon" mode="aspectFill" ></image> ?? ??? ??? ??? ??? ?<text>{{item.name}}</text> ?? ??? ??? ??? ?</view> ?? ??? ??? ?</view>
將所需要的參數(shù)直接@click=“toAddress(item.address)”,放在函數(shù)的括號(hào)內(nèi)傳遞。接受的時(shí)候如下:
methods: { ?? ??? ??? ?toAddress (e){ ?? ??? ??? ??? ?console.log(e); ?? ??? ??? ?} ?? ??? ?},
二,通過(guò)自定義屬性傳參,我經(jīng)常用這種。
<view class="order_status"> ?? ??? ??? ??? ??? ?<view class="status" v-for="item in status" @click="toAddress" data-id="1"> ?? ??? ??? ??? ??? ??? ?<image class="icon" :src="item.url" mode="aspectFill"></image> ?? ??? ??? ??? ??? ??? ?<text>{{item.name}}</text> ?? ??? ??? ??? ??? ?</view> ?? ??? ??? ??? ?</view>
通過(guò)自定義屬性data-id將字符串“1”傳遞過(guò)去。(這種方法小程序上經(jīng)常使用)接受的時(shí)候如下:
methods: { ?? ??? ??? ?toAddress (e){ ?? ??? ??? ??? ?console.log(e.currentTarget.dataset.id); ?? ??? ??? ?} ?? ??? ?},
三,將事件本身傳遞過(guò)去。
<view class="order_status"> ?? ??? ??? ??? ??? ?<view class="status" v-for="item in status" @click="toAddress($event)" data-id="1"> ?? ??? ??? ??? ??? ??? ?<image class="icon" :src="item.url" mode="aspectFill"></image> ?? ??? ??? ??? ??? ??? ?<text>{{item.name}}</text> ?? ??? ??? ??? ??? ?</view> ?? ??? ??? ??? ?</view>
methods: { ?? ??? ??? ?toAddress (e){ ?? ??? ??? ??? ?console.log(e); ?? ??? ??? ?} ?? ??? ?},
vue事件函數(shù)傳參
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id="app"> <div>{{num}}</div> <div> <!-- 如果事件直接綁定函數(shù)名稱,那么默認(rèn)會(huì)傳遞事件對(duì)象作為事件函數(shù)的第一個(gè)參數(shù) --> <button v-on:click='handle1'>點(diǎn)擊1</button> <!-- 2、如果事件綁定函數(shù)調(diào)用,那么事件對(duì)象必須作為最后一個(gè)參數(shù)顯示傳遞, 并且事件對(duì)象的名稱必須是$event --> <button v-on:click='handle2(123, 456, $event)'>點(diǎn)擊2</button> </div> </div> <script type="text/javascript" src="js/vue.js"></script> <script type="text/javascript"> var vm = new Vue({ el: '#app', data: { num: 0 }, methods: { handle1: function(event) { console.log(event.target.innerHTML) }, handle2: function(p, p1, event) { console.log(p, p1) console.log(event.target.innerHTML) this.num++; } } }); </script> </body> </html>```
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯(cuò)Failed?to?resolve?import的原因及解決
這篇文章主要給大家介紹了關(guān)于vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯(cuò)Failed?to?resolve?import的原因及解決方案,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Vue3使用hooks函數(shù)實(shí)現(xiàn)代碼復(fù)用詳解
這篇文章主要介紹了Vue3使用hooks函數(shù)實(shí)現(xiàn)代碼復(fù)用詳解,Vue3的hook函數(shù)可以幫助我們提高代碼的復(fù)用性,?讓我們能在不同的組件中都利用hooks函數(shù)2022-06-06vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場(chǎng)景示例詳解
這篇文章主要為大家介紹了Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場(chǎng)景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Vue組件內(nèi)部實(shí)現(xiàn)一個(gè)雙向數(shù)據(jù)綁定的實(shí)例代碼
這篇文章主要介紹了Vue組件內(nèi)部實(shí)現(xiàn)一個(gè)雙向數(shù)據(jù)綁定的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04vue點(diǎn)擊按鈕實(shí)現(xiàn)簡(jiǎn)單頁(yè)面的切換
這篇文章主要為大家詳細(xì)介紹了vue點(diǎn)擊按鈕實(shí)現(xiàn)簡(jiǎn)單頁(yè)面的切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09Vue項(xiàng)目中大文件切片上傳實(shí)現(xiàn)秒傳與斷點(diǎn)續(xù)傳的詳細(xì)實(shí)現(xiàn)過(guò)程
這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中大文件切片上傳實(shí)現(xiàn)秒傳與斷點(diǎn)續(xù)傳的詳細(xì)實(shí)現(xiàn)過(guò)程, 在開(kāi)發(fā)中,如果上傳的文件過(guò)大,可以考慮分片上傳,分片就是說(shuō)將文件拆分來(lái)進(jìn)行上傳,將各個(gè)文件的切片傳遞給后臺(tái),然后后臺(tái)再進(jìn)行合并,需要的朋友可以參考下2023-08-08