Search code examples
javascriptvue.jsvuejs3vue-composition-apiprimevue

Vue composition API calling child component method


TLDR; In v2, this.$refs does the job but how can I do that in v3 composition api?

I am trying to use CustomUpload feature of PrimeVue in Vue3, but that API does not clear the upload files after uploading them and I need to call clear() method of the child component in the parent component to clear the files and refresh the button.

Here's my App.vue

<template>
  <FileUpload
    name="upload"
    url="/"
    mode="basic"
    :auto="true"
    :maxFileSize="26214400"
    :fileLimit="1"
    :customUpload="true"
    @uploader="upload"
  />
  <Button name="lalaal">qweeq</Button>
</template>

<script>
import FileUpload from 'primevue/fileupload'

export default {
  setup() {
    const upload = e => {
      console.log('testing', e)
    }
    return { upload }
  },
  components: {
    FileUpload
  }
}
</script>

Thanks


Solution

  • You could use template ref then use uploadFile.value instead of this.$refs.uploadFile :

    <template>
      <FileUpload
        ref="uploadFile"
        name="upload"
        url="/"
        mode="basic"
        :auto="true"
        :maxFileSize="26214400"
        :fileLimit="1"
        :customUpload="true"
        @uploader="upload"
      />
      <Button name="lalaal">qweeq</Button>
    </template>
    
    <script>
    import FileUpload from 'primevue/fileupload'
    import {ref} from "vue";
    
    export default {
      setup() {
        const uploadFile=ref(null)
     
        
        const upload = e => {
          console.log('testing', e)
        }
        return { upload,uploadFile}
      },
      components: {
        FileUpload
      }
    }
    </script>