A problem encountered in converting pictures to base64?

<input type="file" @change="getFile($event)">

getFile:function(e){
    var base64 = "";
    let file = e.target.files[0];
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        //console.log(e.target.result); 
        base64 = e.target.result;
    }
    console.log(base64); //
}

questions are written in comments


FileReader.onload is asynchronous. When you console.log (base64) , onload hasn't been executed yet


because onload is asynchronous


if Synchronize forcibly recommends es6 promise


console.log (base64);
is written inside onload.
if you need to do something extra after the onload executes successfully, you can use the form of a callback function.
for example:

reader.onload = function (e) {
    base64 = e.target.result;
    console.log(base64)
}
Menu