【发布时间】:2019-05-20 08:39:34
【问题描述】:
我的代码有问题。它说我的 Array 是未定义的,但后来的一个方法可以正常工作。代码如下:
books: IBook[] = [];
[...]
ngOnInit() {
this.service.getEmployees().subscribe(
(listBooks) => this.books = listBooks,
(err) => console.log(err)
);
}
[...]
events: CalendarEvent[] = [
{
start: new Date(),
end: new Date(),
title: ""+this.books[0].device, //this.books[0] is undefined
color: colors.yellow,
actions: this.actions,
resizable: {
beforeStart: true,
afterEnd: true
},
draggable: true
},
[...]
test(){ //this method is linked to a button
console.log(this.books[0]) //after clicking it works fine
}
[...]
Test 方法在数组之后,它说它是“未定义”,所以它不能是未定义的,可以吗?
【问题讨论】:
-
getEmployees是异步的,this.books将在一段时间后分配。 -
书籍:任何 = [];
-
@ErAbdulMeman 感谢您的回答,但没有成功
-
@RobertoZvjerkovic 你知道怎么解决吗?
-
试试这个代码可能会起作用 --- 替换这个标题:""+this.books[0].device, ==> 标题:this.books[0]?" "+ this.books[0].device:' '
标签: arrays angular typescript undefined