在Angular 2模板中,安全操作符?.
可以工作,但在component.ts
中不能使用TypeScript 2.0.此外,安全导航操作员(!.
)不工作.
例如:
这是Typescript
if (a!.b!.c) { }
编译成这个JavaScript
if (a.b.c) { }
但当我运行它时,我会得到以下错误:
无法读取未定义的属性"b"
除了以下内容,还有其他 Select 吗?
if (a && a.b && a.b.c) { }
在Angular 2模板中,安全操作符?.
可以工作,但在component.ts
中不能使用TypeScript 2.0.此外,安全导航操作员(!.
)不工作.
例如:
这是Typescript
if (a!.b!.c) { }
编译成这个JavaScript
if (a.b.c) { }
但当我运行它时,我会得到以下错误:
无法读取未定义的属性"b"
除了以下内容,还有其他 Select 吗?
if (a && a.b && a.b.c) { }
自从TypeScript 3.7发布以来,您现在可以使用可选链接.
Property example:
let x = foo?.bar.baz();
这相当于:
let x = (foo === null || foo === undefined)
? undefined
: foo.bar.baz();
此外,您可以拨打:
Optional Call
function(otherFn: (par: string) => void) {
otherFn?.("some value");
}
只有当otherFn
不等于null
或undefined
时,才会调用otherFn
Usage optional chaining in IF statement
这是:
if (someObj && someObj.someProperty) {
// ...
}
现在可以用这个来代替
if (someObj?.someProperty) {
// ...
}
参考:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html