1

I have a component called BaseFile

components/BaseFile.vue

<template>
  <div>
    <b-form-group :label="label">
      <b-form-file
        :value="value"
        :placeholder="placeholder"
        :accept="acceptedExtensions"
        @input="updateValue">
      </b-form-file>
    </b-form-group>
  </div>
</template>

<script>
  export default {
    name: 'BaseFile',
    props:   {
      label: { type: String },
      value: { type: Object },
      placeholder: { type: String, default: "Choose a file..." },
      acceptedExtensions: { type: String, default: "image/jpeg, image/png" }
    }, 
    methods: {
      updateValue(value) {
        // console.log(typeof value)
        // console.log(event.target.value)
        this.$emit('input', value);
      }
    }
  }
</script>

then I'm calling this component in my Users/new.vue

<BaseFile label="Primary Image" v-model="primaryImage"/>

<script>
import BaseFile from "~/components/UI/BaseFile";
export default {
  components: {
    BaseFile
  },
  data() {
    return {
      profileImage: {}
    }
  }
</script>

when I try to add a file in BaseFile component, the error I'm getting is

 Invalid prop: type check failed for prop "value". Expected Object, got File.

I checked the documentation of Vue and there's no available props for File. But I need file because I'm uploading it directly to s3.

2
  • So the issue is that you're getting different value instead of a File? I actually tested this on my local and did not get this prop check warning. Commented Jan 16, 2019 at 9:24
  • I tried to do instanceof the value from updateValue method and I'm getting File. I tried adding File to the type of value and I'm getting File is not defined Commented Jan 17, 2019 at 3:12

2 Answers 2

3

Vuejs will use instanceof for type checking.

So you can simply add File if you require both type:

  export default {
    name: 'BaseFile',
    props:   {
      label: { type: String },
      value: { type: [Object, File] },
      placeholder: { type: String, default: "Choose a file..." },
      acceptedExtensions: { type: String, default: "image/jpeg, image/png" }
    },
  }

Also my advise would be to initialize profileImage with undefined or null IIUC you are not using it.

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

4 Comments

I'm still getting an error File is undefined when I add the type to [Object, File] and I changed the profileImage to null or undefined.
I made it work by not specifying the type for value: {}
Which browser are you using to have File is undefined ?
I'm using Chrome
0

Since the data you're expecting is of type File you can set the type to both File and Object by passing an array like so

<script>
  export default {
    name: 'BaseFile',
    props:   {
      label: { type: String },
      value: { type: [Object, File},
      placeholder: { type: String, default: "Choose a file..." },
      acceptedExtensions: { type: String, default: "image/jpeg, image/png" }
    }, 
    methods: {
      updateValue(value) {
        // console.log(typeof value)
        // console.log(event.target.value)
        this.$emit('input', value);
      }
    }
  }
</script>

Comments

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.