无法读取未定义的属性-cdkDropListData拖放角度材质

时间:2018-12-21 03:42:14

标签: angular drag-and-drop google-cloud-firestore material subscribe

我正在使用新的Angular 7 Material CDK拖放功能将项目从列表移动到另一个,并且在移动这些对象时视觉上一切都摇晃,但是在列表中更改项目时出现错误,提示无法获取属性(列表数组)来自未定义,即使我使用的是“ item?.array”东西

在构造函数中:

this.userService.getUserById('some-user-id').subscribe(user => {
  this.user1 = user;
  console.log(user1.route.routePoints); // I see the array in log
});

this.userService.getUserById('some-other-user-id').subscribe(user => {
  this.user2 = user;
  console.log(user2.route.routePoints); // I see the array in log
});

在将项目从列表拖动到另一个列表时,出现错误“无法读取未定义的属性'routePoints'”

<div cdkDropList #routeA="cdkDropList" cdkDropListOrientation="horizontal" 
[cdkDropListData]="user1?.route?.routePoints"
class="list" (cdkDropListDropped)="drop($event, false, user1)" 
[cdkDropListConnectedTo]="[queue, routeB]">

这是拖动代码:

drop(event: CdkDragDrop<RoutePoint[]>, fromQueue: boolean = false, user: User) {
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  } else {
    transferArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex);
  }
  this.userService.updateUser(user);
}

2 个答案:

答案 0 :(得分:5)

如marcinolawski @ https://github.com/angular/components/issues/15948所述,原因是:

问题是_DragRef.initialContainer ==未定义。这是因为_DragRef.initializeDragSequence()的调用早于_DragRef.withDropContainer()。 _initializeDragSequence()基于_dropContainer初始化_initialContainer,_dropContainer由_withDropContainer()初始化,但是由于_initializeDragSequence()的调用早于_withDropContainer(),因此_initialContainer设置为undefined。更多信息:https://github.com/angular/components/blob/master/src/cdk/drag-drop/drag-ref.ts

一个丑陋但可行的解决方法是触发AfterViewChecked上的更改检测,如下所示(通过lukaszgodula @与上述相同的github):

ngAfterViewChecked() {
   this.cdRef.detectChanges();
}

当然还要将ChangeDetectorRef注入组件:

constructor(
   private cdRef: ChangeDetectorRef
}

答案 1 :(得分:1)

确保首先初始化 library(tidyverse) as.tibble(df) %>% select(contains("week"),everything()) %>% gather("id","value",8:ncol(.)) %>% ggplot(aes(week16,value,col=id))+geom_line()+ scale_color_manual(values=c("#75D701","#88dba3","#3ac569","#f9a11b","#0080ff","#f9d423","#00dffc")) 数组

user1