【发布时间】:2020-02-22 10:36:22
【问题描述】:
Vue 版本:3.1.1
大家好,
我正在使用动态创建组件,这意味着用户可以添加他想要的任何组件。我根据此文档 dynamic component creation 创建它。
我使用这个组件vue image uploader。
当用户想要上传图片时,我需要发送一个索引,像这样:
<div v-for="(line, index) in lines" v-bind:key="index">
{{index}}//if i log the index its 0,1,2,3 and its ok
...
<image-uploader
:preview="true"
:class-name="['fileinput', { 'fileinput--loaded': line.hasImage }]"
:capture="false"
:debug="0"
:auto-rotate="true"
output-format="blob"
accept="image/*"
@input="setImage(output , index)"
:ref="'fileUpload'+index"
>
...
还有 setImage 功能:
setImage: function(output,index) {
console.log(index);
console.log(output);
return ;
this.lines[index].hasImage = true;
this.lines[index].image = output;
let formData = new FormData();
formData.append("file", output);
Ax.post(upload_route, formData, {
headers: { "Content-Type": "multipart/form-data" }
})
.then(response => {
// upload successful
})
.catch(error => console.log(error));
}
而日志结果是:
索引始终为 0 :(
当我想上传索引时如何发送索引?
我阅读了这个passing event and index 并对其进行了测试,但它不适用于组件。 因为这是自定义事件而不是 DOM 事件。
我该怎么办?
谢谢。
【问题讨论】:
-
@nmfzone 你能看一下吗。
-
有人可以帮忙吗?
-
但是要发送这个索引,你必须有一个索引,你的索引是从哪里来的?有v-for吗?如果您不创建计算属性,甚至不创建将动态发布该索引的方法,则每次调用都会在索引中产生一个值,以便您进行控制。
-
抱歉,您的提及似乎没有通知我。让我看看@meti
-
似乎
@input采用函数名,而不是函数的返回值。由于您正在调用setImage(output, index),因此您实际上是将setImage的返回值传递给@input。
标签: vue.js