First of all, I would not recommend using this approach if you have multiple elements with unique props for each element, what you should be doing is to create an array that contains multiple objects, each object includes the data needed for that canvas. for example:
data(){
return {
canvases:[
{
class: 'class-name',
imgSrc: 'image-source',
width: 800,
height: 600,
....
},
{
class: 'class-name-2',
imgSrc: 'image-source-2',
width: 800,
height: 600,
}
....
]
}
}
and then loop through this array:
<!-- 5 elements -->
<polygonCrop
v-for="(canvas, i) in canvases"
:key="i"
:canvasClass="canvas.class"
:height="canvas.height"
:imageSource="canvas.imgSrc"
:showCanvas="show(i)"
:showPointer="showPointer"
:width="800"
:ref="'canvas-' + i"
></polygonCrop>
However if you wanna keep your code structure the way it is, here is a solution for you
For the DOM, use a v-for
loop with dynamic ref binding, for the image sources use a method to match the data variables name:
<!-- 5 elements -->
<polygonCrop :canvasClass="'some-class'"
:height="600"
:imageSource="getSource('imgSrc' + n)"
:showCanvas="show"
:showPointer="showPointer"
:width="800"
v-for="n in 5"
:key="n"
:ref="'canvas-' + n"
></polygonCrop>
<b-button @click.prevent="crop(n)" variant="success" v-for="n in 5">Crop {{n}}</b-button>
and use only one crop function, with a parameter:
// methods
crop: function (n) {
const canvas = 'canvas-' + n
this.$refs[canvas].crop();
this.resultImage = this.$refs[canvas].resultImage;
this.show = false;
this.showResult = true;
},
getSource : function(img) {
return this[img]
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…