vue中如何通過函數(shù)傳參數(shù)
vue通過函數(shù)傳參數(shù)
一,通過點(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ù)的括號內(nèi)傳遞。接受的時候如下:
methods: {
?? ??? ??? ?toAddress (e){
?? ??? ??? ??? ?console.log(e);
?? ??? ??? ?}
?? ??? ?},二,通過自定義屬性傳參,我經(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>通過自定義屬性data-id將字符串“1”傳遞過去。(這種方法小程序上經(jīng)常使用)接受的時候如下:
methods: {
?? ??? ??? ?toAddress (e){
?? ??? ??? ??? ?console.log(e.currentTarget.dataset.id);
?? ??? ??? ?}
?? ??? ?},三,將事件本身傳遞過去。
<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)會傳遞事件對象作為事件函數(shù)的第一個參數(shù) -->
<button v-on:click='handle1'>點(diǎn)擊1</button>
<!-- 2、如果事件綁定函數(shù)調(diào)用,那么事件對象必須作為最后一個參數(shù)顯示傳遞,
并且事件對象的名稱必須是$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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯Failed?to?resolve?import的原因及解決
這篇文章主要給大家介紹了關(guān)于vue3+vite+vant項(xiàng)目下按需引入vant報(bào)錯Failed?to?resolve?import的原因及解決方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Vue3使用hooks函數(shù)實(shí)現(xiàn)代碼復(fù)用詳解
這篇文章主要介紹了Vue3使用hooks函數(shù)實(shí)現(xiàn)代碼復(fù)用詳解,Vue3的hook函數(shù)可以幫助我們提高代碼的復(fù)用性,?讓我們能在不同的組件中都利用hooks函數(shù)2022-06-06
vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場景示例詳解
這篇文章主要為大家介紹了Vue實(shí)現(xiàn)Excel預(yù)覽功能使用場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Vue組件內(nèi)部實(shí)現(xiàn)一個雙向數(shù)據(jù)綁定的實(shí)例代碼
這篇文章主要介紹了Vue組件內(nèi)部實(shí)現(xiàn)一個雙向數(shù)據(jù)綁定的實(shí)例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
vue點(diǎn)擊按鈕實(shí)現(xiàn)簡單頁面的切換
這篇文章主要為大家詳細(xì)介紹了vue點(diǎn)擊按鈕實(shí)現(xiàn)簡單頁面的切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09
Vue項(xiàng)目中大文件切片上傳實(shí)現(xiàn)秒傳與斷點(diǎn)續(xù)傳的詳細(xì)實(shí)現(xiàn)過程
這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中大文件切片上傳實(shí)現(xiàn)秒傳與斷點(diǎn)續(xù)傳的詳細(xì)實(shí)現(xiàn)過程, 在開發(fā)中,如果上傳的文件過大,可以考慮分片上傳,分片就是說將文件拆分來進(jìn)行上傳,將各個文件的切片傳遞給后臺,然后后臺再進(jìn)行合并,需要的朋友可以參考下2023-08-08

