vue:调用带有额外参数的函数

时间:2018-06-28 01:25:36

标签: javascript vue.js vuejs2 vue-component element-ui

https://github.com/ElemeFE/element http://element.eleme.io/#/en-US/component/upload

<el-upload
  class="avatar-uploader"
  action="/upload"
  :show-file-list="false"
  :on-error="handleUrlError"
  :on-success="handleUrlSuccess">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

功能:

handleUrlSuccess(response, file, fileList) {
}

如果添加额外的参数:

<el-upload
  class="avatar-uploader"
  action="/upload"
  :show-file-list="false"
  :on-error="handleUrlError"
  :on-success="handleUrlSuccess(response, file, fileList, 233)">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

功能:

handleUrlSuccess(response, file, fileList, param) {
}

属性或方法“响应”未在实例上定义,但在渲染期间被引用。

1 个答案:

答案 0 :(得分:0)

方法的参数来自组件的发出的成功事件。例如,它会有类似的

this.$emit('on-success', response, file, fileList)

在内部,Vue会做类似的事情(这非常简化)...

let boundEventHandler = parseEventHandlerExpression(eventName)
boundEventFunction.apply(vmInstance, arguments)

您可以在消费者中执行的操作是捕获所有这些参数,然后附加自己的参数

:on-success="handleUrlSuccess(...arguments, 233)"