角度8拖放CDK中的DOM异常

我正在使用角度cdk创建一个简单的拖放应用程序。我只想拖我的 框并将其放到另一个框。但是当释放拖放区域内的拖动框时,出现了错误。请帮助修复它。

我遇到错误

Unhandled Promise rejection: Failed to execute 'appendChild' on 'Node': The new child element contains the parent. ; Zone: <root> ; Task: Promise.then ; Value: DOMException: Failed to execute 'appendChild' on 'Node': The new child element contains the parent.
    at DragRef._cleanupDragArtifacts (http://localhost:4200/vendor.js:1615:121)
    at http://localhost:4200/vendor.js:1471:22
    at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:3365:26)
    at Zone.run (http://localhost:4200/polyfills.js:3130:43)
    at http://localhost:4200/polyfills.js:3861:36
    at ZoneDelegate.invoketask (http://localhost:4200/polyfills.js:3397:31)
    at Zone.runTask (http://localhost:4200/polyfills.js:3174:47)
    at drainmicroTaskQueue (http://localhost:4200/polyfills.js:3565:35)
    at Zonetask.invoketask [as invoke] (http://localhost:4200/polyfills.js:3475:21)
    at invoketask (http://localhost:4200/polyfills.js:4609:14) Error: Failed to execute 'appendChild' on 'Node': The new child element contains the parent.
    at DragRef._cleanupDragArtifacts (http://localhost:4200/vendor.js:1615:121)
    at http://localhost:4200/vendor.js:1471:22
    at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:3365:26)
    at Zone.run (http://localhost:4200/polyfills.js:3130:43)
    at http://localhost:4200/polyfills.js:3861:36
    at ZoneDelegate.invoketask (http://localhost:4200/polyfills.js:3397:31)
    at Zone.runTask (http://localhost:4200/polyfills.js:3174:47)
    at drainmicroTaskQueue (http://localhost:4200/polyfills.js:3565:35)
    at Zonetask.invoketask [as invoke] (http://localhost:4200/polyfills.js:3475:21)
    at invoketask (http://localhost:4200/polyfills.js:4609:14)
api.onUnhandledError @ zone-evergreen.js:651
handleUnhandledRejection @ zone-evergreen.js:675
api.microtaskDrainDone @ zone-evergreen.js:668
drainmicroTaskQueue @ zone-evergreen.js:566
invoketask @ zone-evergreen.js:469
invoketask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629

我将在下面粘贴我的代码

我的app.component.html

<section>
  <div class="wrapper">
    <div class="outerBox">
        <div class="innerBox" cdkDropList [cdkDropListConnectedTo]="[dropZone]" cdkDrag></div>
    </div>

    <div class="outerBox" #dropZone="cdkDropList" cdkDropList (cdkDropListDropped)="onItemDrop($event)">
    </div>
  </div>
</section>

和app.component.ts

import { Component } from '@angular/core';
import {cdkDragDrop,moveItemInArray,transferArrayItem} from '@angular/cdk/drag-drop';
@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent {

  onItemDrop(event) {
    console.log(event);
  }
}

和app.component.css文件类似

.wrapper {
    display: flex;
}
.outerBox {
    width: 300px;
    height: 500px;
    border: 1px solid #333;
    padding: 10px;
    margin: 10px;
}
.innerBox {
    width: 100px;
    height: 100px;
    background: skyblue;
}

请帮助解决错误。

谢谢。

speedstar119 回答:角度8拖放CDK中的DOM异常

您可以按照以下步骤开始操作。

在组件类中

  1. 定义2个列表以相互连接。
  2. 并实现 moveItemInArray transferArrayItem cdk帮助器方法:第一种方法是处理每个列表中的更改索引。第二个处理从列表到另一个列表的转移。

在模板中:

  1. 将每个列表绑定到参考或ID(例如#listRef)容器
  2. 为每个
  3. 添加 cdkDropList cdkDropListData cdkDropListConnectedTo 指令
  4. 添加方法来监听两个列表的 cdkDropListDropped ,此处为“ onItemDrop”
  5. 在每个列表上
  6. 迭代,并对每个可拖动项使用 cdkDrag 指令

我准备了一个有效的堆叠Topic Alias

本文链接:https://www.f2er.com/3161711.html

大家都在问