【问题标题】:Vue.js Send an index with @input eventVue.js 使用 @input 事件发送索引
【发布时间】: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


【解决方案1】:

因为您实际上是将setImage 的返回值传递给@input,而不是方法。

您不能只向setImage 添加额外的参数,因为ImageUploader 组件只会向setImage 发出image。如果您需要向该方法添加额外的参数,则需要创建包装 ImageUploader 的自定义元素。

是这样的:

ImageUpload.vue

<template>
  <image-uploader
    :debug="0"
    :autoRotate="true"
    outputFormat="blob"
    :preview="true"
    :className="['fileinput', { 'fileinput--loaded' : hasImage }]"
    :capture="false"
    accept="image/*"
    doNotResize="['gif', 'svg']"
    @input="setImage"
    v-on="listeners" />
</template>

<script>
export default {
  props: {
    index: {
      required: true,
      type: Number
    }
  },
  data() {
    return {
      hasImage: false,
      image: null
    };
  },
  computed: {
    listeners() {
      const listeners = { ...this.$listeners };

      const customs = ["input"];

      customs.forEach(name => {
        if (listeners.hasOwnProperty(name)) {
          delete listeners[name];
        }
      });

      return listeners;
    }
  },
  methods: {
    setImage(image) {
      this.hasImage = true;
      this.image = image;

      this.$emit("input", this.index, image); // here, we emit two params, as index for the first argument, and the image at the second argument
    }
  }
};
</script>

然后,您可以像这样使用该组件:

<template>
  <div class="container">
    <div v-for="(line, index) in lines" :key="index">
      <image-upload :index="index" @input="setImage"/>
    </div>
  </div>
</template>

<script>
import ImageUpload from "./ImageUpload";

export default {
  components: {
    ImageUpload
  },
  data() {
    return {
      lines: ["1", "2", "3", "4"]
    };
  },
  methods: {
    setImage(index, image) {
      console.log("Result", index, image);
    }
  }
};
</script>

查看工作示例:https://codesandbox.io/s/vue-template-ccn0e

【讨论】:

  • 它在工作吗@meti?至少添加评论以确认它是否有效。
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 2019-02-22
  • 2017-09-12
  • 2018-05-01
  • 2019-11-16
  • 1970-01-01
  • 2015-11-02
  • 2020-08-19
相关资源
最近更新 更多