gpt4 book ai didi

angular - 错误类型错误 : Cannot read property 'split' of null in angular custom pipe

转载 作者:行者123 更新时间:2023-12-04 16:09:45 24 4
gpt4 key购买 nike

我收到此错误 ERROR TypeError: Cannot read property 'split' of null error while using this angular pipe,这是代码。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'fullDate'
})
export class DatePipe implements PipeTransform {

transform(value:any ) {
const dateArray = value.split('-');
const date = dateArray[2].substr(0, 1) === '0' ? dateArray[2].substr(1, 1) : dateArray[2];
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

return `${date} ${months[dateArray[1] - 1]} ${dateArray[0]}`;
}

}

{{ lorem?.lorem_date | fullDate }}

enter image description here

最佳答案

该错误意味着您正在拆分一个空值,因此您只需添加一个检查即可。

像这样尝试:

transform(value:any ) {
if(value){
const dateArray = value.split('-');
const date = dateArray[2].substr(0, 1) === '0' ? dateArray[2].substr(1, 1) : dateArray[2];
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

return `${date} ${months[dateArray[1] - 1]} ${dateArray[0]}`;
}
}

关于angular - 错误类型错误 : Cannot read property 'split' of null in angular custom pipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59437160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com