简体   繁体   English

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

[英]vue:call a function with extra param

https://github.com/ElemeFE/element http://element.eleme.io/#/en-US/component/upload 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>

function:功能:

handleUrlSuccess(response, file, fileList) {
}

if add a extra param:如果添加额外的参数:

<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>

function:功能:

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

Property or method "response" is not defined on the instance but referenced during render.属性或方法“响应”未在实例上定义,而是在渲染期间引用。

The arguments for your method are coming from the component's emitted on-success event.您的方法的参数来自组件发出的成功事件。 For example, it would have something like例如,它会有类似的东西

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

Internally, Vue would do something like (and this is very simplified)...在内部,Vue 会做类似的事情(这是非常简化的)...

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

What you can do in your consumer is capture all these arguments and then append your own您可以在消费者中做的是捕获所有这些参数,然后附加您自己的

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

or或者

:on-success="(res, file, fileList) => handleUrlSuccess(res, file, fileList, 233)"

Thanks woki for that last one感谢woki的最后一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM