Angular 如何获取离子2中的返回值?
我有两个函数,我试图让其他函数的返回值继续,但返回值始终未定义 这是我返回值的代码Angular 如何获取离子2中的返回值?,angular,typescript,ionic2,ionic3,Angular,Typescript,Ionic2,Ionic3,我有两个函数,我试图让其他函数的返回值继续,但返回值始终未定义 这是我返回值的代码 export class HomePage { variables={ a:1, b:2, c:3, } constructor(public navCtrl: NavController) { this.function1(); } function1(){ if(function2()){ alert("true");
export class HomePage {
variables={
a:1,
b:2,
c:3,
}
constructor(public navCtrl: NavController) {
this.function1();
}
function1(){
if(function2()){
alert("true");
}else{
alert("false");
}
}
function2(){
if(this.variables.a + this.variables.b >= this.variables.c){
return true;
}else{
return false;
}
}
}
由于您将这两个函数声明为组件的一部分,因此需要使用
this
关键字来执行它们:
function1(){
if(this.function2()){ // <--- like this!
alert("true");
}else{
alert("false");
}
}
function1(){
如果(this.function2()){//这是组件的一部分吗?你能给我们看一下整个组件代码吗?@sebafereras,我用完整的代码保存了对问题的编辑谢谢,请告诉我答案是否解决了问题:)谢谢,是的,错误解决了,但警报总是显示“false”。当我检查console.log(this.function2())时;然后显示“undefined”(未定义),我用完全相同的代码(使用函数的this
关键字)创建的,并且似乎工作正常。你能看一下吗?我很抱歉。我在编辑代码时出错了。工作正常。非常感谢你,听到这个消息很高兴:)