I'm creating a reusable component for v-file-input
of Vuetify.
Form.vue
<BaseFile v-model="image" label="Upload Image"/>
<script>
export default {
name: 'Form',
data() {
return {
image: []
}
}
}
</script>
BaseFile.vue
<v-file-input
:value="value"
:label="label"
solo
show-size
@change="updateValue">
</v-file-input>
<script>
export default {
name: 'BaseFile',
props: {
label: { type: String },
value: { type: Array }
},
methods: {
updateValue(file) {
if (file.name) {
const reader = new FileReader();
reader.onload = e => {
const base64Data = e.target.result;
const uploadFile = [{ fileName: file.name, base64: base64Data }];
this.$emit('input', uploadFile);
}
reader.readAsDataURL(file);
}
}
}
}
</script>
The image value becomes
[
{ fileName: 'hello.png', base64: '.....'}
]
But I'm getting a
TypeError: Cannot read property 'length' of undefined"
v-file-input
havetruncateText
function, which acceptsstr
. Your component provides it only for first call, and for second 2 it raise error, coz try to get length of undefined. – bonusrkimage
becomes the array of object – AllenCv-file-input
methods, you'll see that yourBaseFile
tries to truncateText multiply times, but provides string for it only once/ – bonusrk