【问题标题】:vue:call a function with extra paramvue:调用带有额外参数的函数
【发布时间】:2018-12-06 23:30:03
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    您的方法的参数来自组件发出的 on-success 事件。例如,它会有类似

    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最后一个

    【讨论】:

    • 渲染错误:在中发现“SyntaxError:JSON 输入意外结束”
    • @woki 如果没有 a) 看到该错误消息的其余部分,以及 b) 它引用的代码,将无法帮助您解决这个问题
    • :on-success="(response, file, fileList) => {handleUrlSuccess(response, file, fileList, 233) }"
    猜你喜欢
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2018-10-18
    相关资源
    最近更新 更多