我正在使用nativescript
上传图像,但是无法将图像base64
返回到视图进行预览,这里的问题是我的函数无法读取全局变量。 / p>
这是代码
myImg:any;
pickFiles() {
let options = {
title: "Importer",
cancelButtonText: "Annuler",
actions: ["Image(s)", "Video(s)", "Audio(s)", "Fichier(s)"]
};
dialogs.action(options).then(async (result) => {
if (result == 'Image(s)') {...}
if (result == 'Video(s)') {...}
if (result == 'Audio(s)') {...}
if (result == 'Fichier(s)') {...}
this.mdf.on("getFiles", async function (res: any) {
let results = res.object.get('results');
let img1 = await imageSourceModule.fromFile(results[0].file).toBase64String('jpg');
// let img2 = await imageSourceModule.fromBase64(img1)
this.myImg;
});
this.mdf.on("error", function (res) {
let msg = res.object.get('msg');
console.log(msg);
});
});
我希望变量myImg
获得img
的新值,但是当我绑定它以查看它时,它就为空。
答案 0 :(得分:0)
myImg1
将不会被引用,并且您必须在控制台上遇到错误。原因是匿名函数将拥有它自己的this
并且不会引用组件的this
。因此,您需要具有箭头功能:
您需要像这样修改代码:
pickFiles() {
let options = {
title: "Importer",
cancelButtonText: "Annuler",
actions: ["Image(s)", "Video(s)", "Audio(s)", "Fichier(s)"]
};
dialogs.action(options).then(async (result) => {
if (result == 'Image(s)') {...}
if (result == 'Video(s)') {...}
if (result == 'Audio(s)') {...}
if (result == 'Fichier(s)') {...}
this.mdf.on("getFiles", async (res: any) => {
let results = res.object.get('results');
let img1 = await imageSourceModule.fromFile(results[0].file).toBase64String('jpg');
// let img2 = await imageSourceModule.fromBase64(img1)
this.myImg = img1;
});
this.mdf.on("error", function (res) {
let msg = res.object.get('msg');
console.log(msg);
});
});