与 Null Undefined 连接在 Angular Typescript 中不起作用
Concatenate with Null Undefined not Working in Angular Typescript
我正在尝试连接单词,同时删除 null 和 undefined。
目前在下图中仍然出现错误。有人会如何解决这个问题?
javascript-string-concatenation-behavior-with-null-or-undefined-values
在图片中,其中一个属性是这样说的
TypeError: cannot read property of undefined
它甚至不能首先读取参数,应用未定义的过滤器等
get copyFromDescription() {
if (this.addressId == 0) {
return 'Select';
} else { return [this.apn, this.combinedAddress,this.addressType.addressTypeDescription].filter(Boolean).join('') }
}
数据值图片
使用
.filter(val => !!val)
布尔是一种类型
并测试未定义的 addressType
this.addressType && this.addressType.addressTypeDescription
get copyFromDescription() {
if (this.addressId == 0) {
return 'Select';
} else { return [this.apn, this.combinedAddress,this.addressType && this.addressType.addressTypeDescription].filter(val => !!val).join('') }
}
this.addressType 未定义。因此在尝试访问 addressTypeDescription 属性.
时无法获取值
布尔值是一种类型而不是测试。这对我来说似乎更合乎逻辑。
get copyFromDescription() {
if (this.addressId == 0) {
return 'Select';
} else { return [this.apn, this.combinedAddress,this.addressType.addressTypeDescription].filter(val => !!val).join('') }
}
但是你仍然会遇到需要测试的未定义错误。
get copyFromDescription() {
if (this.addressId === 0)
return 'Select';
else if (this.addressType) return [this.apn, this.combinedAddress, this.addressType.addressTypeDescription]
.filter(val => !!val).join('');
else return [this.apn, this.combinedAddress] // you cannot use addressType here
.filter(val => !!val).join('');
}
我正在尝试连接单词,同时删除 null 和 undefined。 目前在下图中仍然出现错误。有人会如何解决这个问题?
javascript-string-concatenation-behavior-with-null-or-undefined-values
在图片中,其中一个属性是这样说的
TypeError: cannot read property of undefined
它甚至不能首先读取参数,应用未定义的过滤器等
get copyFromDescription() {
if (this.addressId == 0) {
return 'Select';
} else { return [this.apn, this.combinedAddress,this.addressType.addressTypeDescription].filter(Boolean).join('') }
}
数据值图片
使用
.filter(val => !!val)
布尔是一种类型
并测试未定义的 addressType
this.addressType && this.addressType.addressTypeDescription
get copyFromDescription() {
if (this.addressId == 0) {
return 'Select';
} else { return [this.apn, this.combinedAddress,this.addressType && this.addressType.addressTypeDescription].filter(val => !!val).join('') }
}
this.addressType 未定义。因此在尝试访问 addressTypeDescription 属性.
时无法获取值布尔值是一种类型而不是测试。这对我来说似乎更合乎逻辑。
get copyFromDescription() {
if (this.addressId == 0) {
return 'Select';
} else { return [this.apn, this.combinedAddress,this.addressType.addressTypeDescription].filter(val => !!val).join('') }
}
但是你仍然会遇到需要测试的未定义错误。
get copyFromDescription() {
if (this.addressId === 0)
return 'Select';
else if (this.addressType) return [this.apn, this.combinedAddress, this.addressType.addressTypeDescription]
.filter(val => !!val).join('');
else return [this.apn, this.combinedAddress] // you cannot use addressType here
.filter(val => !!val).join('');
}