1

I am trying to upload a file with vue, but the issue I am stuck with is this, I can't access this.imageFile.value after selecting the photo. The data I returned from setup but undefined.

Under normal conditions, imageFile.value works in setup.

<template>
  <img v-show="imageUrl" :src="imageUrl" alt="">
  <input @change="handleImageSelected,getdata" ref="fileInput" type="file" accept="image/*">
</template>
<script>
import {UseImageUpload} from "./use/UseImageUpload";

export default {
  data() {
    return {
      Hello: null
    }
  },
  methods: {
    getdata() {
      this.Hello = this.imageFile.value
    }
  },
  setup() {
    let {imageFile, imageUrl, handleImageSelected} = UseImageUpload();

    return {
      imageFile,
      handleImageSelected,
      imageUrl,
    }
  }
}
</script>

UseImageUpload.js

import {ref, watch} from "vue";

export function UseImageUpload() {
//image
    let imageFile = ref("");
    let imageUrl = ref("");

    function handleImageSelected(event) {
        if (event.target.files.length === 0) {
            imageFile.value = "";
            imageUrl.value = "";
            return;
        }

        imageFile.value = event.target.files[0];
    }

    watch(imageFile, (imageFile) => {
        if (!(imageFile instanceof File)) {
            return;
        }

        let fileReader = new FileReader();

        fileReader.readAsDataURL(imageFile);

        fileReader.addEventListener("load", () => {
            imageUrl.value = fileReader.result;
        });
    });

    return {
        imageFile,
        imageUrl,
        handleImageSelected,
    };
}
3
  • why are you mixing up Options and Composition API and what exactly is this.Hello? Commented Apr 26, 2022 at 8:12
  • like this, when input file is uploaded. Use Image Upload data is being entered and I will create data for the user with the incoming data. Commented Apr 26, 2022 at 8:15
  • try my answer if it does not work just leave a comment and I will take a further look Commented Apr 26, 2022 at 8:18

1 Answer 1

1

First of all please try not to mix Options and Composition API - I know it might work but it is not necessary and in the most cases just an anti-pattern.
Composition API is there to replace the Options API or rather to give an alternative. They are just not supposed to work together or to be used together.

So this would improve your code:

setup() {
    const Hello = ref(null);
    const {imageFile, imageUrl, handleImageSelected} = UseImageUpload();

    function getdata() {
      Hello = imageFile.value
    }

    return {
      imageFile,
      handleImageSelected,
      imageUrl,
      getdata,
}

This should also fix your issue.

Sign up to request clarification or add additional context in comments.

4 Comments

What problems can there be when mixed?
There might be no problems at all but the code looks better, is easier to read and the chance of getting an error increases because you might mix the two up. For example .value is only needed in setup and therefore you might use .value in Options API and this is just an error you could prevent with only using Composition API (or only using Options API)
You may be right, but I have a situation like this. in the options api in data ` categories: { title: '', slug: '', seo: { title: '', dec: '' }, language: 'en', }, ` There is a yield like this, how can I access it in the composition api.
the same as in my answer. You would just write: const categories = ref({ title: '', slug: '', seo: { title: '', dec: '' }, language: 'en', }). And to access it in setup you would just need to write categories.value and to use it in the template you just have to return it at the end of setup and access it in the template just as categories. Fort further information just look at the documentation vuejs.org/guide/introduction.html and set the left switch to "Composition"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.