[英]How to correctly use a method in a method in a Vue component?
我正在使用exifjs从图像中获取GPS数据。 我正在尝试将lat和long变量转换为十进制变量。 像这样:
<template lang="html">
<div class="upload-wrap">
<button class="btn">Kies een foto</button>
<input ref="fileinput" @change="onChange" id="file-input" type="file" accept="image/jpeg"/>
</div>
</template>
<script>
import EXIF from '../../node_modules/exif-js/exif'
export default {
methods: {
toDecimal(number) {
return number[0].numerator + number[1].numerator /
(60 * number[1].denominator) + number[2].numerator / (3600 * number[2].denominator);
},
onChange(image) {
var input = this.$refs.fileinput
if (image) {
EXIF.getData(input.files[0], function() {
var lat = EXIF.getTag(this, 'GPSLatitude');
var long = EXIF.getTag(this, 'GPSLongitude');
if (lat && long) {
var lat_dec = toDecimal(lat);
var long_dec = toDecimal(long);
// eslint-disable-next-line
console.log(lat_dec, long_dec);
}
else {
// No metadata found
clearFileInput(input);
alert("Geen GPS data gevonden in afbeelding '" + input.files[0].name + "'.");
}
})
} else {
// eslint-disable-next-line
console.log(`Geen afbeelding?`);
}
},
// Clear file input if there's no exif data
clearFileInput(ctrl) {
ctrl.value = null;
}
}
}
</script>
但我收到以下错误:
ReferenceError: toDecimal is not defined
我使用的语法不正确吗?或者我忘记了什么?
编辑:我尝试使用this.toDecimal(lat);
但这会导致TypeError: this.toDecimal is not a function
您可以拨打this.toDecimal
但在这种情况下, this
回调是不是Vue的实例。 您可以在var self = this
使用箭头功能或小技巧
onChange(image) {
var input = this.$refs.fileinput
var self = this;
if (image) {
EXIF.getData(input.files[0], function() {
var lat = EXIF.getTag(this, 'GPSLatitude');
var long = EXIF.getTag(this, 'GPSLongitude');
if (lat && long) {
var lat_dec = self.toDecimal(lat);
var long_dec = self.toDecimal(long);
// eslint-disable-next-line
console.log(lat_dec, long_dec);
} else {
// No metadata found
clearFileInput(input);
alert("Geen GPS data gevonden in afbeelding '" + input.files[0].name + "'.");
}
})
} else {
// eslint-disable-next-line
console.log(`Geen afbeelding?`);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.