簡體   English   中英

vue:調用帶有額外參數的函數

[英]vue:call a function with extra param

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) {
}

屬性或方法“響應”未在實例上定義,而是在渲染期間引用。

您的方法的參數來自組件發出的成功事件。 例如,它會有類似的東西

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

在內部,Vue 會做類似的事情(這是非常簡化的)...

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

您可以在消費者中做的是捕獲所有這些參數,然后附加您自己的

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

或者

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

感謝woki的最后一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM