错误类型错误:无法读取 Angular 7 拖放中未定义的属性“长度”

Posted

技术标签:

【中文标题】错误类型错误:无法读取 Angular 7 拖放中未定义的属性“长度”【英文标题】:ERROR TypeError: Cannot read property 'length' of undefined in Angular 7 Drag and Drop 【发布时间】:2019-04-02 20:02:30 【问题描述】:

我只是想在@angular/cdk/drag-dropAngular 7 DragDropModule 的帮助下创建拖放组件。但我总是收到如下错误。

HomeComponent.html:14 ERROR TypeError: Cannot read property 'length' of undefined
    at moveItemInArray (drag-drop.es5.js:1287)
    at HomeComponent.push../src/app/home/home.component.ts.HomeComponent.drop (home.component.ts:31)
    at Object.eval [as handleEvent] (HomeComponent.html:15)
    at handleEvent (core.js:19676)
    at callWithDebugContext (core.js:20770)
    at Object.debugHandleEvent [as handleEvent] (core.js:20473)
    at dispatchEvent (core.js:17125)
    at core.js:18615
    at SafeSubscriber.schedulerFn [as _next] (core.js:10218)
    at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196)

这是我的 HomeComponent.html

<div style="display: flex;">
  <div class="container">
    <div class="row">
      <h2 style="text-align: center">Movies</h2>
      <div  cdkDropList #allmovies="cdkDropList" [cdkDropListData]="movies" [cdkDropListConnectedTo]="[towatch]"
        (cdkDropListDropped)="drop($event)">
        <app-movie *ngFor="let movie of movies" [movie]="movie" cdkDrag></app-movie>
      </div>
    </div>
  </div>
  <div class="container">
    <div class="row">
      <h2 style="text-align: center">Movies to watch</h2>
      <div cdkDropList #towatch="cdkDropList" [cdkDropListConnectedTo]="[allmovies]"
        (cdkDropListDropped)="drop($event)">
        <app-movie *ngFor="let movie of moviesToWatch" [movie]="movie" cdkDrag></app-movie>
      </div>
    </div>
  </div>
</div>

还有 HomeComponent.ts

export class HomeComponent 
  movies: Movie[];
  moviesToWatch: Movie[] = [
    poster_path: '/uC6TTUhPpQCmgldGyYveKRAu8JN.jpg'
  ];
  constructor(private movieService: MovieService) 
    this.movieService.get(config.api.topRated)
      .subscribe((data) => 
        this.formatDta(JSON.parse(data._body).results);
      );
  
  formatDta(_body: Movie[]): void 
    this.movies = _body.filter(movie => movie.poster_path !== '/uC6TTUhPpQCmgldGyYveKRAu8JN.jpg');
  
  drop(event: CdkDragDrop<string[]>) 
    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);
    
  

我知道我错过了一些东西。但不确定,它是什么。非常感谢任何帮助。

【问题讨论】:

【参考方案1】:

我发现这里有什么问题。如果您注意到第二个容器的代码,在 div cdkDropList #toWatch="cdkDropList" 我缺少属性 [cdkDropListData] 并且它是强制性的,所以我必须设置 [cdkDropListData]="moviesToWatch" 如下。

<div cdkDropList #toWatch="cdkDropList" [cdkDropListData]="moviesToWatch" [cdkDropListConnectedTo]="[allmovies]"
        (cdkDropListDropped)="drop($event)">
        <app-movie class="example-box" *ngFor="let movie of moviesToWatch" [movie]="movie" cdkDrag>movie</app-movie>
      </div>

设置后,一切都按预期工作。

【讨论】:

以上是关于错误类型错误:无法读取 Angular 7 拖放中未定义的属性“长度”的主要内容,如果未能解决你的问题,请参考以下文章

如何在拖放中停止 dragend 的默认行为?

Angular 8:错误类型错误:无法读取未定义的属性“无效”

Angular 5:错误类型错误:无法读取未定义的属性“偏移量”

错误类型错误:无法读取未定义 Angular 6 的属性“mData”

Angular 6 - 错误类型错误:无法读取未定义的属性“firstName”,同时显示输出

在 Angular 2 中使用 React,错误类型错误:无法读取未定义的属性“渲染”