ERROR TypeError: Cannot read property 'split' of null in angular custom pipe
ERROR TypeError: Cannot read property 'split' of null in angular custom pipe
我收到此错误错误类型错误:使用此 angular 管道时无法读取 属性 'split' 的空错误,这是代码。
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 }}
该错误意味着您正在拆分一个空值,因此您只需添加一个检查即可。
这样试试:
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]}`;
}
}
在管道中添加一个 null
检查:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'fullDate'
})
export class DatePipe implements PipeTransform {
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]}`;
}
return value;
}
}
或
将内容放在 *ngIf
:
<p *ngIf="lorem">{{ lorem.lorem_date | fullDate }}</p>
您的代码中基本上发生的是当它是 运行 时第一次值变量没有分配任何值。因为因为它 return 和错误说 Cannot read property 'split' of null
验证管道内的值以获得值就可以了。请在下面找到示例代码:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'fullDate'
})
export class DatePipe implements PipeTransform {
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 管道时无法读取 属性 'split' 的空错误,这是代码。
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 }}
该错误意味着您正在拆分一个空值,因此您只需添加一个检查即可。
这样试试:
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]}`;
}
}
在管道中添加一个 null
检查:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'fullDate'
})
export class DatePipe implements PipeTransform {
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]}`;
}
return value;
}
}
或
将内容放在 *ngIf
:
<p *ngIf="lorem">{{ lorem.lorem_date | fullDate }}</p>
您的代码中基本上发生的是当它是 运行 时第一次值变量没有分配任何值。因为因为它 return 和错误说 Cannot read property 'split' of null
验证管道内的值以获得值就可以了。请在下面找到示例代码:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'fullDate'
})
export class DatePipe implements PipeTransform {
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]}`;
}
}