我有一个角度为 6 的项目。我想从子方法中停止代码。例如在下面的例子中,如果 studentNo
大于 15 那么我不想运行 alert("studentAge")
。换句话说,我想停止 calculate
方法中的代码。我怎样才能做到这一点?
doSomething(studentAge){
this.calculate(studentAge);
alert(studentAge);
}
calculate(studentAge){
if(studentAge > 15){ return; }
else{ studentAge += 20 }
}
最佳答案
TypeScript 通过值 传递参数。
在 calculate()
中修改 studentAge
不会修改它在 doSomething()
中的值,因为 calculate()
收到的是 studentAge
的值。因此,此方法不执行任何操作。
这是我猜你真正想做的:
doSomething(studentAge: number) {
studentAge = this.calculate(studentAge);
if (studentAge <= 15) {
alert(studentAge);
}
}
calculate(studentAge: number): number {
if (studentAge > 15) {
return studentAge;
}
else {
return studentAge + 20;
}
}
关于typescript - Angular 6中子方法的停止(返回)代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775769/