Typescript 为什么变量是未定义的,即使它是';什么是定义?
我正在使用Ionic框架为移动设备创建应用程序。正如你们所看到的,我创建了加载条,我想在所有事情完成后关闭它。但我也想改变我的布尔人。但console说它们还没有定义。为什么?Typescript 为什么变量是未定义的,即使它是';什么是定义?,typescript,Typescript,我正在使用Ionic框架为移动设备创建应用程序。正如你们所看到的,我创建了加载条,我想在所有事情完成后关闭它。但我也想改变我的布尔人。但console说它们还没有定义。为什么? export class AfterEditPage { public loaded_image = base64Image; public showMainContent: boolean = true; // I am interacting with this one public sho
export class AfterEditPage {
public loaded_image = base64Image;
public showMainContent: boolean = true; // I am interacting with this one
public showAdditionalContent: boolean = false; // and this one
public processing_Result: string = "";
constructor(public loadingctrl: LoadingController, public alertctrl: AlertController, public navCtrl: NavController, public navParams: NavParams) {
}
recognizeImage() {
let resultText;
console.log(resultText);
let loader = this.loadingctrl.create({
content: 'Processing...'
});
console.log(this.processing_Result);
loader.present().then(() => {
Tesseract.recognize(this.loaded_image)
.progress(function (p) {})
.then(function (result) {
resultText = result.text; console.log(resultText);
loader.dismiss().then(() => {
this.showMainContent = !this.showMainContent; // undefined
this.showAdditionalContent = !this.showAdditionalContent; // undefined
});
});
});
}
}
您需要使用箭头函数,因此需要将代码更改为
loader.present().then(() => {
Tesseract.recognize(this.loaded_image)
.progress((p) => {})
.then((result) => {
resultText = result.text; console.log(resultText);
loader.dismiss().then(() => {
this.showMainContent = !this.showMainContent;
this.showAdditionalContent = !this.showAdditionalContent;
});
});
});
}
}
两个词:.Arrow functions here
then(function(result){
this
是全局对象,而不是您的AfterEditPage
@alexanderbird的实例谢谢,这确实帮助了我复制