Vue - Advanced Cropper (Uncaught TypeError: this.$refs.cropper.getResult is not a function)
P粉054616867
P粉054616867 2023-12-14 15:01:09
0
2
442

I have a Vue Advanced Cropper's cropping function as shown below:

crop() { const { canvas } = this.$refs.cropper.getResult(); if (canvas) { const form = new FormData(); canvas.toBlob(blob => { form.append('files[]', blob); // Perhaps you should add the setting appropriate file format here }, 'image/jpeg'); this.formData = form; } },

My HTML:

Step 2: Crop images

{{ image.type }}

I've included the import for Cropper:

import {Cropper} from 'vue-advanced-cropper'

Version in package.json:

"vue-advanced-cropper": "^2.8.1"

Everything works fine until I get to the crop function where it says the following:

Uncaught TypeError: this.$refs.cropper.getResult is not a function

My first thought was that it might have something to do with looping through multiple images, but it doesn't work with just one image either. I tried combining the parts from the disc and uploading to the server from here: https://norserium.github.io/vue-advanced-cropper/guides/recipes.html#upload-image-to-a-server

--- edit ---

I also exported the default value, and the cropping worked, but I just didn’t get the result:

export default { components: { Cropper, },


P粉054616867
P粉054616867

reply all (2)
P粉401527045

aleksKamb found the right solution. After applying the index it seems to be working. I edited v-for to:

           
{{ image.type }}

Then I edited the crop function to:

crop(index) { const { canvas } = this.$refs.cropper[index].getResult(); if (canvas) { const form = new FormData(); canvas.toBlob(blob => { form.append('files[]', blob); // Perhaps you should add the setting appropriate file format here }, 'image/jpeg'); } },
    P粉949848849

    Considering that you are using the same reference name for every element in the v-for, this.$refs.cropper is probably an array. This also depends on your Vue version. If this is the case, you may have to pass arguments to your clipping function so that the index of the calling element is known and getResult can be called correctly.

    Try to console log this.$refs. Also see if this thread is useful?v-for Vue.js reference inside the loopp>

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!