javascript - 从 observable 返回值以查看问题
问题描述
我正在使用 进行图像上传nativescript
,但我无法将图像返回base64
到我的视图进行预览,这里的问题是我的函数没有读取我的全局变量。
这是代码
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
,但是当我将它绑定到查看时它变为空。
解决方案
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);
});
});
推荐阅读
- java - @Autowired Spring 组件在 CustomWebSecurityExpressionRoot 内抛出 null
- c# - Xamarin表单中内容扩展时如何使Frame扩展
- xamarin - Xamarin Forms Lottie 图像未显示
- c# - 从轮廓生成 HALCON 区域
- python - 如何在 python 的 scherrer 方程中拟合高斯模型?
- java - 如何从 ifPresentOrElse 语句中获取对象数据?
- javascript - 如何使时刻(日期)成为对象键,时刻(时间)是值;
- angular - Angular ControlValueAccessor 的 WriteValue 方法不会触发 ngModelChange
- android-fragments - 如何在 Android 的单个片段中使用多个视图模型?
- java - Java:将 ms word 转换为 pdf 时,标题空间中不需要的空格