欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中如何通過(guò)函數(shù)傳參數(shù)

 更新時(shí)間:2023年04月06日 08:37:37   作者:前端小問(wèn)題  
這篇文章主要介紹了vue中如何通過(guò)函數(shù)傳參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評(píng)論