在 Vue 回调函数中传入新的参数,获取不到原有的返回参数

... 2022-11-17 Less than 1 minute

# 在 Vue 回调函数中传入新的参数,获取不到原有的返回参数

例如


// 没有传参
<el-upload :on-success="handleAvatarSuccess"  />

// 传参数
<el-table>
 <el-table-colunm>
    <template slot-scope="scope">
      <el-upload :on-success="handleAvatarSuccess2(scope.row.name)"  />
    </template>
 </el-table-column>
</el-table>

<script>
export default {
    data () {
        return {}
    },
    methods: {
        // 默认返回这些参数
        handleAvatarSuccess(response, file, files) {

        },
        // 默认返回的参数获取不到了
        handleAvatarSuccess2(response) {

        }
    }
}
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

解决方案

  1. 使用 arguments 获取参数
  2. 回调函数包裹一层函数,在外层函数中将参数传给回调函数
// 方法 1
<el-table>
 <el-table-colunm>
    <template slot-scope="scope">
      <el-upload :on-success="handleAvatarSuccess(arguments, scope.row.name)"  />
    </template>
 </el-table-column>
</el-table>

// 方法 2
<el-table>
 <el-table-colunm>
    <template slot-scope="scope">
      <el-upload :on-success="(res, file, files) => handleAvatarSuccess2(res, file, files, scope.row.name)"  />
    </template>
 </el-table-column>
</el-table>


<script>
export default {
    data () {
        return {}
    },
    methods: {
        // 方法 1
        handleAvatarSuccess(arg, res) {

        },
        // 方法 2
        handleAvatarSuccess2(res, file, files, val) {

        }
    }
}
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Last update: November 17, 2022 07:05
Contributors: Skr