在 Angular / Type Script 中,?. 是一种叫做'安全导航运算符”的语法,用于解决对象为空时的异常问题。它可以在访问对象属性或方法时自动检测对象是否为空,避免因对象为空而引发的异常。示例如下:
// 假设 person 为一个可能为空的对象 const name = person?.name; // 如果 person 为空,则 name 为 undefined const age = person?.getAge?.(); // 如果 person 为空或者 person 没有 getAge 方法,则 age 为 undefined,否则为 getAge 方法的返回值